pc.c 370 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101
  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 "quest.h"
  32. #include <stdlib.h>
  33. #include <math.h>
  34. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  35. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  36. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  37. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  38. static unsigned int statp[MAX_LEVEL+1];
  39. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  40. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  41. #endif
  42. // h-files are for declarations, not for implementations... [Shinomori]
  43. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  44. // timer for night.day implementation
  45. int day_timer_tid = INVALID_TIMER;
  46. int night_timer_tid = INVALID_TIMER;
  47. struct eri *pc_sc_display_ers = NULL;
  48. struct eri *pc_itemgrouphealrate_ers = NULL;
  49. int pc_expiration_tid = INVALID_TIMER;
  50. struct fame_list smith_fame_list[MAX_FAME_LIST];
  51. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  52. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  53. #define MOTD_LINE_SIZE 128
  54. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  55. /**
  56. * Translation table from athena equip index to aegis bitmask
  57. */
  58. unsigned int equip_bitmask[EQI_MAX] = {
  59. EQP_ACC_L, // EQI_ACC_L
  60. EQP_ACC_R, // EQI_ACC_R
  61. EQP_SHOES, // EQI_SHOES
  62. EQP_GARMENT, // EQI_GARMENT
  63. EQP_HEAD_LOW, // EQI_HEAD_LOW
  64. EQP_HEAD_MID, // EQI_HEAD_MID
  65. EQP_HEAD_TOP, // EQI_HEAD_TOP
  66. EQP_ARMOR, // EQI_ARMOR
  67. EQP_HAND_L, // EQI_HAND_L
  68. EQP_HAND_R, // EQI_HAND_R
  69. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  70. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  71. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  72. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  73. EQP_AMMO, // EQI_AMMO
  74. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  75. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  76. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  77. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  78. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  79. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  80. };
  81. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  82. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  83. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  84. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  85. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  86. };
  87. /**
  88. * Item Cool Down Delay Saving
  89. * Struct item_cd is not a member of struct map_session_data
  90. * to keep cooldowns in memory between player log-ins.
  91. * All cooldowns are reset when server is restarted.
  92. **/
  93. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  94. struct item_cd {
  95. unsigned int tick[MAX_ITEMDELAYS]; //tick
  96. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  97. };
  98. /**
  99. * Converts a class to its array index for CLASS_COUNT defined arrays.
  100. * Note that it does not do a validity check for speed purposes, where parsing
  101. * player input make sure to use a pcdb_checkid first!
  102. * @param class_
  103. * @return Class Index
  104. */
  105. int pc_class2idx(int class_) {
  106. if (class_ >= JOB_NOVICE_HIGH)
  107. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  108. return class_;
  109. }
  110. /**
  111. * Get player's group ID
  112. * @param sd
  113. * @return Group ID
  114. */
  115. inline int pc_get_group_id(struct map_session_data *sd) {
  116. return sd->group_id;
  117. }
  118. /** Get player's group Level
  119. * @param sd
  120. * @return Group Level
  121. */
  122. inline int pc_get_group_level(struct map_session_data *sd) {
  123. return sd->group_level;
  124. }
  125. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  126. {
  127. struct map_session_data *sd;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if(sd->invincible_timer != tid){
  131. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  132. return 0;
  133. }
  134. sd->invincible_timer = INVALID_TIMER;
  135. skill_unit_move(&sd->bl,tick,1);
  136. return 0;
  137. }
  138. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  139. nullpo_retv(sd);
  140. if( sd->invincible_timer != INVALID_TIMER )
  141. delete_timer(sd->invincible_timer,pc_invincible_timer);
  142. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  143. }
  144. void pc_delinvincibletimer(struct map_session_data* sd)
  145. {
  146. nullpo_retv(sd);
  147. if( sd->invincible_timer != INVALID_TIMER )
  148. {
  149. delete_timer(sd->invincible_timer,pc_invincible_timer);
  150. sd->invincible_timer = INVALID_TIMER;
  151. skill_unit_move(&sd->bl,gettick(),1);
  152. }
  153. }
  154. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  155. {
  156. struct map_session_data *sd;
  157. int i;
  158. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  159. return 1;
  160. if( sd->spiritball <= 0 )
  161. {
  162. 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);
  163. sd->spiritball = 0;
  164. return 0;
  165. }
  166. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  167. if( i == sd->spiritball )
  168. {
  169. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  170. return 0;
  171. }
  172. sd->spiritball--;
  173. if( i != sd->spiritball )
  174. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  175. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  176. clif_spiritball(&sd->bl);
  177. return 0;
  178. }
  179. /**
  180. * Adds a spiritball to player for 'interval' ms
  181. * @param sd
  182. * @param interval
  183. * @param max
  184. */
  185. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  186. {
  187. int tid;
  188. uint8 i;
  189. nullpo_retv(sd);
  190. if(max > MAX_SPIRITBALL)
  191. max = MAX_SPIRITBALL;
  192. if(sd->spiritball < 0)
  193. sd->spiritball = 0;
  194. if( sd->spiritball && sd->spiritball >= max )
  195. {
  196. if(sd->spirit_timer[0] != INVALID_TIMER)
  197. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  198. sd->spiritball--;
  199. if( sd->spiritball != 0 )
  200. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  201. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  202. }
  203. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  204. 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);
  205. if( i != sd->spiritball )
  206. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  207. sd->spirit_timer[i] = tid;
  208. sd->spiritball++;
  209. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  210. clif_millenniumshield(&sd->bl,sd->spiritball);
  211. else
  212. clif_spiritball(&sd->bl);
  213. }
  214. /**
  215. * Removes number of spiritball from player
  216. * @param sd
  217. * @param count
  218. * @param type 1 = doesn't give client effect
  219. */
  220. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  221. {
  222. uint8 i;
  223. nullpo_retv(sd);
  224. if(sd->spiritball <= 0) {
  225. sd->spiritball = 0;
  226. return;
  227. }
  228. if(count == 0)
  229. return;
  230. if(count > sd->spiritball)
  231. count = sd->spiritball;
  232. sd->spiritball -= count;
  233. if(count > MAX_SPIRITBALL)
  234. count = MAX_SPIRITBALL;
  235. for(i=0;i<count;i++) {
  236. if(sd->spirit_timer[i] != INVALID_TIMER) {
  237. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  238. sd->spirit_timer[i] = INVALID_TIMER;
  239. }
  240. }
  241. for(i=count;i<MAX_SPIRITBALL;i++) {
  242. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  243. sd->spirit_timer[i] = INVALID_TIMER;
  244. }
  245. if(!type) {
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. }
  252. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  253. int *c, *b_sd;
  254. struct block_list *src;
  255. struct map_session_data *tsd;
  256. struct status_change *sc;
  257. nullpo_ret(bl);
  258. nullpo_ret(tsd = (struct map_session_data*)bl);
  259. nullpo_ret(src = va_arg(ap,struct block_list *));
  260. c = va_arg(ap,int *);
  261. b_sd = va_arg(ap, int *);
  262. if(pc_isdead(tsd))
  263. return 0;
  264. sc = status_get_sc(bl);
  265. if( sc && sc->data[SC_BANDING] )
  266. {
  267. b_sd[(*c)++] = tsd->bl.id;
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  273. int c;
  274. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  275. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  276. int range = skill_get_splash(LG_BANDING,skill_lv);
  277. nullpo_ret(sd);
  278. c = 0;
  279. memset(b_sd, 0, sizeof(b_sd));
  280. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  281. if( c < 1 ) //just recalc status no need to recalc hp
  282. { // No more Royal Guards in Banding found.
  283. struct status_change *sc;
  284. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  285. {
  286. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  287. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  288. }
  289. return 0;
  290. }
  291. //Add yourself
  292. hp = status_get_hp(&sd->bl);
  293. i++;
  294. // Get total HP of all Royal Guards in party.
  295. for( j = 0; j < i; j++ ){
  296. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  297. if( bsd != NULL )
  298. hp += status_get_hp(&bsd->bl);
  299. }
  300. // Set average HP.
  301. hp = hp / i;
  302. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  303. for( j = 0; j < i; j++ )
  304. {
  305. int tmp_hp=0;
  306. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  307. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  308. extra_hp += tmp_hp;
  309. tmp_qty++;
  310. }
  311. }
  312. if( extra_hp > 0 && tmp_qty > 0 )
  313. hp += extra_hp / tmp_qty;
  314. for( j = 0; j < i; j++ ){
  315. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  316. if( bsd != NULL )
  317. {
  318. struct status_change *sc;
  319. status_set_hp(&bsd->bl,hp,0); // Set hp
  320. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  321. {
  322. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  323. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  324. }
  325. }
  326. }
  327. return c;
  328. }
  329. /**
  330. * Increases a player's fame points and displays a notice to him
  331. * @param sd Player
  332. * @param count Fame point
  333. */
  334. void pc_addfame(struct map_session_data *sd,int count)
  335. {
  336. int ranktype=-1;
  337. nullpo_retv(sd);
  338. sd->status.fame += count;
  339. if(sd->status.fame > MAX_FAME)
  340. sd->status.fame = MAX_FAME;
  341. switch(sd->class_&MAPID_UPPERMASK){
  342. case MAPID_BLACKSMITH: ranktype = 0; break;
  343. case MAPID_ALCHEMIST: ranktype = 1; break;
  344. case MAPID_TAEKWON: ranktype = 2; break;
  345. }
  346. clif_update_rankingpoint(sd,ranktype,count);
  347. chrif_updatefamelist(sd);
  348. }
  349. /**
  350. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  351. * @param sd
  352. * @param job Job use enum e_mapid
  353. * @return Rank
  354. */
  355. unsigned char pc_famerank(uint32 char_id, int job)
  356. {
  357. uint8 i;
  358. switch(job){
  359. case MAPID_BLACKSMITH: // Blacksmith
  360. for(i = 0; i < MAX_FAME_LIST; i++){
  361. if(smith_fame_list[i].id == char_id)
  362. return i + 1;
  363. }
  364. break;
  365. case MAPID_ALCHEMIST: // Alchemist
  366. for(i = 0; i < MAX_FAME_LIST; i++){
  367. if(chemist_fame_list[i].id == char_id)
  368. return i + 1;
  369. }
  370. break;
  371. case MAPID_TAEKWON: // Taekwon
  372. for(i = 0; i < MAX_FAME_LIST; i++){
  373. if(taekwon_fame_list[i].id == char_id)
  374. return i + 1;
  375. }
  376. break;
  377. }
  378. return 0;
  379. }
  380. /**
  381. * Restart player's HP & SP value
  382. * @param sd
  383. * @param type Restart type: 1 - Normal Resurection
  384. */
  385. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  386. struct status_data *status, *b_status;
  387. nullpo_retv(sd);
  388. b_status = &sd->base_status;
  389. status = &sd->battle_status;
  390. if (type&1) { //Normal resurrection
  391. status->hp = 1; //Otherwise status_heal may fail if dead.
  392. status_heal(&sd->bl, b_status->hp, 0, 1);
  393. if( status->sp < b_status->sp )
  394. status_set_sp(&sd->bl, b_status->sp, 1);
  395. } else { //Just for saving on the char-server (with values as if respawned)
  396. sd->status.hp = b_status->hp;
  397. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  398. }
  399. }
  400. /*==========================================
  401. Rental System
  402. *------------------------------------------*/
  403. /**
  404. * Ends a rental and removes the item/effect
  405. * @param tid: Tick ID
  406. * @param tick: Timer
  407. * @param id: Timer ID
  408. * @param data: Data
  409. * @return false - failure, true - success
  410. */
  411. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  412. {
  413. struct map_session_data *sd = map_id2sd(id);
  414. if( sd == NULL )
  415. return 0;
  416. if( tid != sd->rental_timer ) {
  417. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  418. return 0;
  419. }
  420. pc_inventory_rentals(sd);
  421. return 1;
  422. }
  423. /**
  424. * Removes the rental timer from the player
  425. * @param sd: Player data
  426. */
  427. void pc_inventory_rental_clear(struct map_session_data *sd)
  428. {
  429. if( sd->rental_timer != INVALID_TIMER ) {
  430. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  431. sd->rental_timer = INVALID_TIMER;
  432. }
  433. }
  434. /**
  435. * Check for items in the player's inventory that are rental type
  436. * @param sd: Player data
  437. */
  438. void pc_inventory_rentals(struct map_session_data *sd)
  439. {
  440. int i, c = 0;
  441. unsigned int expire_tick, next_tick = UINT_MAX;
  442. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  443. if( sd->status.inventory[i].nameid == 0 )
  444. continue; // Nothing here
  445. if( sd->status.inventory[i].expire_time == 0 )
  446. continue;
  447. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  448. if (sd->inventory_data[i]->unequip_script)
  449. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  450. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  451. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  452. } else {
  453. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  454. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  455. next_tick = umin(expire_tick, next_tick);
  456. c++;
  457. }
  458. }
  459. 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
  460. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  461. else
  462. sd->rental_timer = INVALID_TIMER;
  463. }
  464. /**
  465. * Add a rental item to the player and adjusts the rental timer appropriately
  466. * @param sd: Player data
  467. * @param seconds: Rental time
  468. */
  469. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  470. {
  471. unsigned int tick = seconds * 1000;
  472. if( sd == NULL )
  473. return;
  474. if( sd->rental_timer != INVALID_TIMER ) {
  475. const struct TimerData * td;
  476. td = get_timer(sd->rental_timer);
  477. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  478. pc_inventory_rental_clear(sd);
  479. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  480. }
  481. } else
  482. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  483. }
  484. /**
  485. * Determines if player can give / drop / trade / vend items
  486. */
  487. bool pc_can_give_items(struct map_session_data *sd)
  488. {
  489. return pc_has_permission(sd, PC_PERM_TRADE);
  490. }
  491. /**
  492. * Determines if player can give / drop / trade / vend bounded items
  493. */
  494. bool pc_can_give_bounded_items(struct map_session_data *sd)
  495. {
  496. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  497. }
  498. /*==========================================
  499. * Prepares character for saving.
  500. * @param sd
  501. *------------------------------------------*/
  502. void pc_makesavestatus(struct map_session_data *sd) {
  503. nullpo_retv(sd);
  504. if(!battle_config.save_clothcolor)
  505. sd->status.clothes_color = 0;
  506. // Since this is currently not officially released,
  507. // its best to have a forced option to not save body styles.
  508. if(!battle_config.save_body_style)
  509. sd->status.body = 0;
  510. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  511. //status change load/saving. [Skotlex]
  512. #ifdef NEW_CARTS
  513. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  514. #else
  515. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  516. #endif
  517. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  518. if(pc_isdead(sd)){
  519. pc_setrestartvalue(sd, 0);
  520. } else {
  521. sd->status.hp = sd->battle_status.hp;
  522. sd->status.sp = sd->battle_status.sp;
  523. }
  524. sd->status.last_point.map = sd->mapindex;
  525. sd->status.last_point.x = sd->bl.x;
  526. sd->status.last_point.y = sd->bl.y;
  527. return;
  528. }
  529. if(pc_isdead(sd)) {
  530. pc_setrestartvalue(sd, 0);
  531. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  532. } else {
  533. sd->status.hp = sd->battle_status.hp;
  534. sd->status.sp = sd->battle_status.sp;
  535. sd->status.last_point.map = sd->mapindex;
  536. sd->status.last_point.x = sd->bl.x;
  537. sd->status.last_point.y = sd->bl.y;
  538. }
  539. if(map[sd->bl.m].flag.nosave) {
  540. struct map_data *m=&map[sd->bl.m];
  541. if(m->save.map)
  542. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  543. else
  544. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  545. }
  546. }
  547. /*==========================================
  548. * Off init ? Connection?
  549. *------------------------------------------*/
  550. 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) {
  551. nullpo_retv(sd);
  552. sd->bl.id = account_id;
  553. sd->status.account_id = account_id;
  554. sd->status.char_id = char_id;
  555. sd->status.sex = sex;
  556. sd->login_id1 = login_id1;
  557. sd->login_id2 = 0; // at this point, we can not know the value :(
  558. sd->client_tick = client_tick;
  559. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  560. sd->bl.type = BL_PC;
  561. sd->canlog_tick = gettick();
  562. //Required to prevent homunculus copuing a base speed of 0.
  563. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  564. }
  565. /**
  566. * Get equip point for an equip
  567. * @param sd
  568. * @param n Equip index in inventory
  569. */
  570. int pc_equippoint(struct map_session_data *sd,int n){
  571. int ep = 0;
  572. nullpo_ret(sd);
  573. if(!sd->inventory_data[n])
  574. return 0;
  575. if (!itemdb_isequip2(sd->inventory_data[n]))
  576. return 0; //Not equippable by players.
  577. ep = sd->inventory_data[n]->equip;
  578. if(sd->inventory_data[n]->look == W_DAGGER ||
  579. sd->inventory_data[n]->look == W_1HSWORD ||
  580. sd->inventory_data[n]->look == W_1HAXE) {
  581. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  582. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  583. return EQP_ARMS;
  584. }
  585. return ep;
  586. }
  587. /**
  588. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  589. * @param sd : player session
  590. * @return 0 sucess, 1:invalid sd
  591. */
  592. void pc_setinventorydata(struct map_session_data *sd)
  593. {
  594. uint8 i;
  595. nullpo_retv(sd);
  596. for(i = 0; i < MAX_INVENTORY; i++) {
  597. unsigned short id = sd->status.inventory[i].nameid;
  598. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  599. }
  600. }
  601. /**
  602. * 'Calculates' weapon type
  603. * @param sd : player
  604. */
  605. void pc_calcweapontype(struct map_session_data *sd)
  606. {
  607. nullpo_retv(sd);
  608. // single-hand
  609. if(sd->weapontype2 == W_FIST) {
  610. sd->status.weapon = sd->weapontype1;
  611. return;
  612. }
  613. if(sd->weapontype1 == W_FIST) {
  614. sd->status.weapon = sd->weapontype2;
  615. return;
  616. }
  617. // dual-wield
  618. sd->status.weapon = 0;
  619. switch (sd->weapontype1){
  620. case W_DAGGER:
  621. switch (sd->weapontype2) {
  622. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  623. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  624. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  625. }
  626. break;
  627. case W_1HSWORD:
  628. switch (sd->weapontype2) {
  629. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  630. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  631. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  632. }
  633. break;
  634. case W_1HAXE:
  635. switch (sd->weapontype2) {
  636. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  637. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  638. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  639. }
  640. }
  641. // unknown, default to right hand type
  642. if (!sd->status.weapon)
  643. sd->status.weapon = sd->weapontype1;
  644. }
  645. /**
  646. * Set equip index
  647. * @param sd : Player
  648. */
  649. void pc_setequipindex(struct map_session_data *sd)
  650. {
  651. uint16 i;
  652. nullpo_retv(sd);
  653. for (i = 0; i < EQI_MAX; i++)
  654. sd->equip_index[i] = -1;
  655. for (i = 0; i < MAX_INVENTORY; i++) {
  656. if (sd->status.inventory[i].nameid <= 0)
  657. continue;
  658. if (sd->status.inventory[i].equip) {
  659. uint8 j;
  660. for (j = 0; j < EQI_MAX; j++)
  661. if (sd->status.inventory[i].equip & equip_bitmask[j])
  662. sd->equip_index[j] = i;
  663. if (sd->status.inventory[i].equip & EQP_HAND_R) {
  664. if (sd->inventory_data[i])
  665. sd->weapontype1 = sd->inventory_data[i]->look;
  666. else
  667. sd->weapontype1 = 0;
  668. }
  669. if( sd->status.inventory[i].equip & EQP_HAND_L ) {
  670. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  671. sd->weapontype2 = sd->inventory_data[i]->look;
  672. else
  673. sd->weapontype2 = 0;
  674. }
  675. }
  676. }
  677. pc_calcweapontype(sd);
  678. }
  679. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  680. //{
  681. // int i;
  682. // struct item *item = &sd->status.inventory[eqindex];
  683. // struct item_data *data;
  684. //
  685. // //Crafted/made/hatched items.
  686. // if (itemdb_isspecial(item->card[0]))
  687. // return 1;
  688. //
  689. // /* scan for enchant armor gems */
  690. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  691. // s = MAX_SLOTS - 1;
  692. //
  693. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  694. // return( i < s ) ? 0 : 1;
  695. //}
  696. /**
  697. * Check if an item is equiped by player
  698. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  699. * @param sd : player session
  700. * @param nameid : itemid
  701. * @return 1:yes, 0:no
  702. */
  703. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  704. {
  705. uint8 i;
  706. for( i = 0; i < EQI_MAX; i++ )
  707. {
  708. short index = sd->equip_index[i], j;
  709. if( index < 0 )
  710. continue;
  711. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  712. continue;
  713. if( !sd->inventory_data[index] )
  714. continue;
  715. if( sd->inventory_data[index]->nameid == nameid )
  716. return true;
  717. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  718. if( sd->status.inventory[index].card[j] == nameid )
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. /**
  725. * Check adoption rules
  726. * @param p1_sd: Player 1
  727. * @param p2_sd: Player 2
  728. * @param b_sd: Player that will be adopted
  729. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  730. * ADOPT_ALREADY_ADOPTED - Already adopted
  731. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  732. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  733. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  734. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  735. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  736. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  737. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  738. */
  739. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  740. {
  741. if( !p1_sd || !p2_sd || !b_sd )
  742. return ADOPT_CHARACTER_NOT_FOUND;
  743. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  744. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  745. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  746. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  747. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  748. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  749. if( p2_sd->status.party_id != p1_sd->status.party_id )
  750. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  751. // Parents need to have their ring equipped
  752. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  753. return ADOPT_EQUIP_RINGS;
  754. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  755. return ADOPT_EQUIP_RINGS;
  756. // Already adopted a baby
  757. if( p1_sd->status.child || p2_sd->status.child ) {
  758. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  759. return ADOPT_MORE_CHILDREN;
  760. }
  761. // Parents need at least lvl 70 to adopt
  762. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  763. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  764. return ADOPT_LEVEL_70;
  765. }
  766. if( b_sd->status.partner_id ) {
  767. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  768. return ADOPT_MARRIED;
  769. }
  770. 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 ) )
  771. return ADOPT_NOT_NOVICE;
  772. return ADOPT_ALLOWED;
  773. }
  774. /*==========================================
  775. * Adoption Process
  776. *------------------------------------------*/
  777. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  778. {
  779. int job, joblevel;
  780. unsigned int jobexp;
  781. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  782. return false;
  783. // Preserve current job levels and progress
  784. joblevel = b_sd->status.job_level;
  785. jobexp = b_sd->status.job_exp;
  786. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  787. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  788. { // Success, proceed to configure parents and baby skills
  789. p1_sd->status.child = b_sd->status.char_id;
  790. p2_sd->status.child = b_sd->status.char_id;
  791. b_sd->status.father = p1_sd->status.char_id;
  792. b_sd->status.mother = p2_sd->status.char_id;
  793. // Restore progress
  794. b_sd->status.job_level = joblevel;
  795. clif_updatestatus(b_sd, SP_JOBLEVEL);
  796. b_sd->status.job_exp = jobexp;
  797. clif_updatestatus(b_sd, SP_JOBEXP);
  798. // Baby Skills
  799. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  800. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  801. // Parents Skills
  802. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  803. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  804. return true;
  805. }
  806. return false; // Job Change Fail
  807. }
  808. /*==========================================
  809. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  810. Returns:
  811. false : Cannot use/equip
  812. true : Can use/equip
  813. * Credits:
  814. [Inkfish] for first idea
  815. [Haru] for third-classes extension
  816. [Cydh] finishing :D
  817. *------------------------------------------*/
  818. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  819. while (1) {
  820. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  821. break;
  822. #ifndef RENEWAL
  823. //allow third classes to use trans. class items
  824. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  825. break;
  826. //third-baby classes can use same item too
  827. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  828. break;
  829. //don't need to decide specific rules for third-classes?
  830. //items for third classes can be used for all third classes
  831. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  832. break;
  833. #else
  834. //trans. classes (exl. third-trans.)
  835. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  836. break;
  837. //baby classes (exl. third-baby)
  838. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  839. break;
  840. //third classes (exl. third-trans. and baby-third)
  841. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  842. break;
  843. //trans-third classes
  844. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  845. break;
  846. //third-baby classes
  847. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  848. break;
  849. #endif
  850. return false;
  851. }
  852. return true;
  853. }
  854. /*=================================================
  855. * Checks if the player can equip the item at index n in inventory.
  856. * @param sd
  857. * @param n Item index in inventory
  858. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  859. *------------------------------------------------*/
  860. uint8 pc_isequip(struct map_session_data *sd,int n)
  861. {
  862. struct item_data *item;
  863. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  864. item = sd->inventory_data[n];
  865. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  866. return ITEM_EQUIP_ACK_OK;
  867. if(item == NULL)
  868. return ITEM_EQUIP_ACK_FAIL;
  869. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  870. return ITEM_EQUIP_ACK_FAILLEVEL;
  871. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  872. return ITEM_EQUIP_ACK_FAILLEVEL;
  873. if(item->sex != 2 && sd->status.sex != item->sex)
  874. return ITEM_EQUIP_ACK_FAIL;
  875. if (sd->sc.count) {
  876. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  877. return ITEM_EQUIP_ACK_FAIL;
  878. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  879. return ITEM_EQUIP_ACK_FAIL;
  880. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  881. return ITEM_EQUIP_ACK_FAIL;
  882. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  883. return ITEM_EQUIP_ACK_FAIL;
  884. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  885. return ITEM_EQUIP_ACK_FAIL;
  886. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  887. return ITEM_EQUIP_ACK_FAIL;
  888. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  889. //Spirit of Super Novice equip bonuses. [Skotlex]
  890. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  891. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  892. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  893. switch(item->look) { //In weapons, the look determines type of weapon.
  894. case W_DAGGER: //All level 4 - Daggers
  895. case W_1HSWORD: //All level 4 - 1H Swords
  896. case W_1HAXE: //All level 4 - 1H Axes
  897. case W_MACE: //All level 4 - 1H Maces
  898. case W_STAFF: //All level 4 - 1H Staves
  899. case W_2HSTAFF: //All level 4 - 2H Staves
  900. return ITEM_EQUIP_ACK_OK;
  901. }
  902. }
  903. }
  904. //fail to equip if item is restricted
  905. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  906. return ITEM_EQUIP_ACK_FAIL;
  907. //Not equipable by class. [Skotlex]
  908. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  909. return ITEM_EQUIP_ACK_FAIL;
  910. if (!pc_isItemClass(sd,item))
  911. return ITEM_EQUIP_ACK_FAIL;
  912. return ITEM_EQUIP_ACK_OK;
  913. }
  914. /*==========================================
  915. * No problem with the session id
  916. * set the status that has been sent from char server
  917. *------------------------------------------*/
  918. 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)
  919. {
  920. int i;
  921. #ifdef BOUND_ITEMS
  922. int j;
  923. int idxlist[MAX_INVENTORY];
  924. #endif
  925. unsigned long tick = gettick();
  926. uint32 ip = session[sd->fd]->client_addr;
  927. sd->login_id2 = login_id2;
  928. sd->group_id = group_id;
  929. /* load user permissions */
  930. pc_group_pc_load(sd);
  931. memcpy(&sd->status, st, sizeof(*st));
  932. if (st->sex != sd->status.sex) {
  933. clif_authfail_fd(sd->fd, 0);
  934. return false;
  935. }
  936. //Set the map-server used job id. [Skotlex]
  937. i = pc_jobid2mapid(sd->status.class_);
  938. if (i == -1) { //Invalid class?
  939. 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);
  940. sd->status.class_ = JOB_NOVICE;
  941. sd->class_ = MAPID_NOVICE;
  942. } else
  943. sd->class_ = i;
  944. // Checks and fixes to character status data, that are required
  945. // in case of configuration change or stuff, which cannot be
  946. // checked on char-server.
  947. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  948. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  949. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  950. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  951. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  952. if(!sd->status.hp) pc_setdead(sd);
  953. sd->state.connect_new = 1;
  954. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  955. sd->invincible_timer = INVALID_TIMER;
  956. sd->npc_timer_id = INVALID_TIMER;
  957. sd->pvp_timer = INVALID_TIMER;
  958. sd->expiration_tid = INVALID_TIMER;
  959. sd->autotrade_tid = INVALID_TIMER;
  960. #ifdef SECURE_NPCTIMEOUT
  961. // Initialize to defaults/expected
  962. sd->npc_idle_timer = INVALID_TIMER;
  963. sd->npc_idle_tick = tick;
  964. sd->npc_idle_type = NPCT_INPUT;
  965. sd->state.ignoretimeout = false;
  966. #endif
  967. sd->canuseitem_tick = tick;
  968. sd->canusecashfood_tick = tick;
  969. sd->canequip_tick = tick;
  970. sd->cantalk_tick = tick;
  971. sd->canskill_tick = tick;
  972. sd->cansendmail_tick = tick;
  973. sd->idletime = last_tick;
  974. for(i = 0; i < MAX_SPIRITBALL; i++)
  975. sd->spirit_timer[i] = INVALID_TIMER;
  976. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  977. sd->autobonus[i].active = INVALID_TIMER;
  978. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  979. sd->autobonus2[i].active = INVALID_TIMER;
  980. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  981. sd->autobonus3[i].active = INVALID_TIMER;
  982. if (battle_config.item_auto_get)
  983. sd->state.autoloot = 10000;
  984. if (battle_config.disp_experience)
  985. sd->state.showexp = 1;
  986. if (battle_config.disp_zeny)
  987. sd->state.showzeny = 1;
  988. #ifdef VIP_ENABLE
  989. if (!battle_config.vip_disp_rate)
  990. sd->disableshowrate = 1;
  991. #endif
  992. if (!(battle_config.display_skill_fail&2))
  993. sd->state.showdelay = 1;
  994. pc_setinventorydata(sd);
  995. pc_setequipindex(sd);
  996. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  997. sd->status.option &= ~OPTION_INVISIBLE;
  998. }
  999. status_change_init(&sd->bl);
  1000. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1001. //Set here because we need the inventory data for weapon sprite parsing.
  1002. status_set_viewdata(&sd->bl, sd->status.class_);
  1003. unit_dataset(&sd->bl);
  1004. sd->guild_x = -1;
  1005. sd->guild_y = -1;
  1006. sd->delayed_damage = 0;
  1007. // Event Timers
  1008. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1009. sd->eventtimer[i] = INVALID_TIMER;
  1010. // Rental Timer
  1011. sd->rental_timer = INVALID_TIMER;
  1012. for( i = 0; i < 3; i++ )
  1013. sd->hate_mob[i] = -1;
  1014. sd->quest_log = NULL;
  1015. sd->num_quests = 0;
  1016. sd->avail_quests = 0;
  1017. sd->save_quest = false;
  1018. sd->count_rewarp = 0;
  1019. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1020. sd->regs.arrays = NULL;
  1021. sd->vars_dirty = false;
  1022. sd->vars_ok = false;
  1023. sd->vars_received = 0x0;
  1024. sd->qi_display = NULL;
  1025. sd->qi_count = 0;
  1026. //warp player
  1027. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1028. 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);
  1029. // try warping to a default map instead (church graveyard)
  1030. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1031. // if we fail again
  1032. clif_authfail_fd(sd->fd, 0);
  1033. return false;
  1034. }
  1035. }
  1036. clif_authok(sd);
  1037. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1038. sd->die_counter=-1;
  1039. //display login notice
  1040. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1041. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1042. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1043. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1044. sd->status.name, sd->status.account_id, sd->status.char_id,
  1045. sd->packet_ver, CONVIP(ip), sd->group_id);
  1046. // Send friends list
  1047. clif_friendslist_send(sd);
  1048. if( !changing_mapservers ) {
  1049. if (battle_config.display_version == 1)
  1050. pc_show_version(sd);
  1051. // Message of the Day [Valaris]
  1052. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1053. if (battle_config.motd_type)
  1054. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  1055. else
  1056. clif_displaymessage(sd->fd, motd_text[i]);
  1057. }
  1058. if (expiration_time != 0)
  1059. sd->expiration_time = expiration_time;
  1060. /**
  1061. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1062. **/
  1063. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1064. }
  1065. /**
  1066. * Check if player have any item cooldowns on
  1067. **/
  1068. pc_itemcd_do(sd,true);
  1069. pc_validate_skill(sd);
  1070. #ifdef BOUND_ITEMS
  1071. // Party bound item check
  1072. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,BOUND_PARTY,idxlist))) { // Party was deleted while character offline
  1073. for(i=0;i<j;i++)
  1074. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  1075. }
  1076. #endif
  1077. /* [Ind] */
  1078. sd->sc_display = NULL;
  1079. sd->sc_display_count = 0;
  1080. // Player has not yet received the CashShop list
  1081. sd->status.cashshop_sent = false;
  1082. sd->last_addeditem_index = -1;
  1083. sd->bonus_script.head = NULL;
  1084. sd->bonus_script.count = 0;
  1085. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1086. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1087. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1088. clif_updatestatus(sd, SP_BASEEXP);
  1089. }
  1090. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1091. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1092. clif_updatestatus(sd, SP_JOBEXP);
  1093. }
  1094. // Request all registries (auth is considered completed whence they arrive)
  1095. intif_request_registry(sd,7);
  1096. return true;
  1097. }
  1098. /*==========================================
  1099. * Closes a connection because it failed to be authenticated from the char server.
  1100. *------------------------------------------*/
  1101. void pc_authfail(struct map_session_data *sd)
  1102. {
  1103. clif_authfail_fd(sd->fd, 0);
  1104. return;
  1105. }
  1106. /**
  1107. * Player register a bl as hatred
  1108. * @param sd : player session
  1109. * @param pos : hate position [0;2]
  1110. * @param bl : target bl
  1111. * @return false:failed, true:success
  1112. */
  1113. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1114. {
  1115. int class_;
  1116. if (!sd || !bl || pos < 0 || pos > 2)
  1117. return false;
  1118. if (sd->hate_mob[pos] != -1)
  1119. { //Can't change hate targets.
  1120. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1121. return false;
  1122. }
  1123. class_ = status_get_class(bl);
  1124. if (!pcdb_checkid(class_)) {
  1125. unsigned int max_hp = status_get_max_hp(bl);
  1126. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1127. return false;
  1128. if (pos != status_get_size(bl))
  1129. return false; //Wrong size
  1130. }
  1131. sd->hate_mob[pos] = class_;
  1132. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1133. clif_hate_info(sd, pos, class_, 1);
  1134. return true;
  1135. }
  1136. /*==========================================
  1137. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1138. *------------------------------------------*/
  1139. void pc_reg_received(struct map_session_data *sd)
  1140. {
  1141. uint8 i;
  1142. sd->vars_ok = true;
  1143. sd->change_level_2nd = pc_readglobalreg(sd, add_str("jobchange_level"));
  1144. sd->change_level_3rd = pc_readglobalreg(sd, add_str("jobchange_level_3rd"));
  1145. sd->die_counter = pc_readglobalreg(sd, add_str("PC_DIE_COUNTER"));
  1146. sd->langtype = pc_readaccountreg(sd, add_str("#langtype"));
  1147. if (msg_checklangtype(sd->langtype,true) < 0)
  1148. sd->langtype = 0; //invalid langtype reset to default
  1149. // Cash shop
  1150. sd->cashPoints = pc_readaccountreg(sd, add_str("#CASHPOINTS"));
  1151. sd->kafraPoints = pc_readaccountreg(sd, add_str("#KAFRAPOINTS"));
  1152. // Cooking Exp
  1153. sd->cook_mastery = pc_readglobalreg(sd, add_str("COOK_MASTERY"));
  1154. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1155. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1156. sd->mission_mobid = pc_readglobalreg(sd, add_str("TK_MISSION_ID"));
  1157. sd->mission_count = pc_readglobalreg(sd, add_str("TK_MISSION_COUNT"));
  1158. }
  1159. if (battle_config.feature_banking)
  1160. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1161. if (battle_config.feature_roulette) {
  1162. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1163. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1164. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1165. }
  1166. sd->roulette.prizeIdx = -1;
  1167. //SG map and mob read [Komurka]
  1168. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1169. uint16 j;
  1170. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1171. sd->feel_map[i].index = j;
  1172. sd->feel_map[i].m = map_mapindex2mapid(j);
  1173. } else {
  1174. sd->feel_map[i].index = 0;
  1175. sd->feel_map[i].m = -1;
  1176. }
  1177. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1178. }
  1179. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1180. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1181. sd->cloneskill_idx = skill_get_index(skid);
  1182. if (sd->cloneskill_idx > 0) {
  1183. sd->status.skill[sd->cloneskill_idx].id = skid;
  1184. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1185. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1186. sd->status.skill[sd->cloneskill_idx].lv = i;
  1187. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1188. }
  1189. }
  1190. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1191. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1192. sd->reproduceskill_idx = skill_get_index(skid);
  1193. if (sd->reproduceskill_idx > 0) {
  1194. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1195. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1196. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1197. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1198. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1199. }
  1200. }
  1201. //Weird... maybe registries were reloaded?
  1202. if (sd->state.active)
  1203. return;
  1204. sd->state.active = 1;
  1205. if (sd->status.party_id)
  1206. party_member_joined(sd);
  1207. if (sd->status.guild_id)
  1208. guild_member_joined(sd);
  1209. // pet
  1210. if (sd->status.pet_id > 0)
  1211. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1212. // Homunculus [albator]
  1213. if( sd->status.hom_id > 0 )
  1214. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1215. if( sd->status.mer_id > 0 )
  1216. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1217. if( sd->status.ele_id > 0 )
  1218. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1219. map_addiddb(&sd->bl);
  1220. map_delnickdb(sd->status.char_id, sd->status.name);
  1221. if (!chrif_auth_finished(sd))
  1222. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1223. pc_load_combo(sd);
  1224. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  1225. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1226. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1227. chrif_bsdata_request(sd->status.char_id);
  1228. sd->storage_size = MIN_STORAGE; //default to min
  1229. #ifdef VIP_ENABLE
  1230. sd->vip.time = 0;
  1231. sd->vip.enabled = 0;
  1232. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0); // request VIP informations
  1233. #endif
  1234. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1235. intif_request_questlog(sd);
  1236. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1237. sd->state.connect_new = 1;
  1238. clif_parse_LoadEndAck(sd->fd, sd);
  1239. }
  1240. if( sd->sc.option&OPTION_INVISIBLE ) {
  1241. sd->vd.class_ = INVISIBLE_CLASS;
  1242. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1243. // decrement the number of pvp players on the map
  1244. map[sd->bl.m].users_pvp--;
  1245. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1246. // unregister the player for ranking
  1247. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1248. sd->pvp_timer = INVALID_TIMER;
  1249. }
  1250. clif_changeoption( &sd->bl );
  1251. }
  1252. pc_check_expiration(sd);
  1253. if( sd->state.autotrade ) {
  1254. clif_parse_LoadEndAck(sd->fd, sd);
  1255. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  1256. }
  1257. }
  1258. static int pc_calc_skillpoint(struct map_session_data* sd)
  1259. {
  1260. uint16 i, skill_point = 0;
  1261. nullpo_ret(sd);
  1262. for(i = 1; i < MAX_SKILL; i++) {
  1263. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1264. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1265. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1266. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1267. )
  1268. {
  1269. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1270. skill_point += sd->status.skill[i].lv;
  1271. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1272. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1273. }
  1274. }
  1275. }
  1276. return skill_point;
  1277. }
  1278. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1279. uint16 i = 0;
  1280. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1281. return false;
  1282. /**
  1283. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1284. * skill tree and since they have no icons they'll give resource errors
  1285. * Get ALL skills except npc/guild ones. [Skotlex]
  1286. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1287. **/
  1288. for( i = 0; i < MAX_SKILL; i++ ) {
  1289. uint16 skill_id = skill_idx2id(i);
  1290. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1291. continue;
  1292. switch (skill_id) {
  1293. case SM_SELFPROVOKE:
  1294. case AB_DUPLELIGHT_MELEE:
  1295. case AB_DUPLELIGHT_MAGIC:
  1296. case WL_CHAINLIGHTNING_ATK:
  1297. case WL_TETRAVORTEX_FIRE:
  1298. case WL_TETRAVORTEX_WATER:
  1299. case WL_TETRAVORTEX_WIND:
  1300. case WL_TETRAVORTEX_GROUND:
  1301. case WL_SUMMON_ATK_FIRE:
  1302. case WL_SUMMON_ATK_WIND:
  1303. case WL_SUMMON_ATK_WATER:
  1304. case WL_SUMMON_ATK_GROUND:
  1305. case LG_OVERBRAND_BRANDISH:
  1306. case LG_OVERBRAND_PLUSATK:
  1307. case WM_SEVERE_RAINSTORM_MELEE:
  1308. case RL_R_TRIP_PLUSATK:
  1309. case SG_DEVIL:
  1310. case MO_TRIPLEATTACK:
  1311. case RG_SNATCHER:
  1312. continue;
  1313. default:
  1314. {
  1315. uint8 lv = (uint8)skill_get_max(skill_id);
  1316. if (lv > 0) {
  1317. sd->status.skill[i].id = skill_id;
  1318. if (addlv)
  1319. sd->status.skill[i].lv = lv;
  1320. }
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. return true;
  1326. }
  1327. /*==========================================
  1328. * Calculation of skill level.
  1329. * @param sd
  1330. *------------------------------------------*/
  1331. void pc_calc_skilltree(struct map_session_data *sd)
  1332. {
  1333. int i, flag;
  1334. int c = 0;
  1335. nullpo_retv(sd);
  1336. i = pc_calc_skilltree_normalize_job(sd);
  1337. c = pc_mapid2jobid(i, sd->status.sex);
  1338. if( c == -1 )
  1339. { //Unable to normalize job??
  1340. 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);
  1341. return;
  1342. }
  1343. c = pc_class2idx(c);
  1344. for( i = 0; i < MAX_SKILL; i++ ) {
  1345. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1346. sd->status.skill[i].id = 0; //First clear skills.
  1347. /* permanent skills that must be re-checked */
  1348. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1349. uint16 sk_id = skill_idx2id(i);
  1350. if (!sk_id) {
  1351. sd->status.skill[i].id = 0;
  1352. sd->status.skill[i].lv = 0;
  1353. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1354. continue;
  1355. }
  1356. switch (sk_id) {
  1357. case NV_TRICKDEAD:
  1358. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1359. sd->status.skill[i].id = 0;
  1360. sd->status.skill[i].lv = 0;
  1361. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1362. }
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. for( i = 0; i < MAX_SKILL; i++ ) {
  1368. uint16 skill_id = 0;
  1369. // Restore original level of skills after deleting earned skills.
  1370. 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 ) {
  1371. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1372. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1373. }
  1374. //Enable Bard/Dancer spirit linked skills.
  1375. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1376. continue;
  1377. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1378. //Link Dancer skills to bard.
  1379. if( sd->status.sex ) {
  1380. if( sd->status.skill[i-8].lv < 10 )
  1381. continue;
  1382. sd->status.skill[i].id = skill_id;
  1383. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1384. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1385. }
  1386. //Link Bard skills to dancer.
  1387. else {
  1388. if( sd->status.skill[i].lv < 10 )
  1389. continue;
  1390. sd->status.skill[i-8].id = skill_id - 8;
  1391. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1392. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1393. }
  1394. }
  1395. }
  1396. // Removes Taekwon Ranker skill bonus
  1397. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1398. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1399. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1400. uint16 sk_id = skill_tree[c_][i].id;
  1401. uint16 sk_idx = 0;
  1402. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].id)))
  1403. continue;
  1404. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1405. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1406. continue;
  1407. sd->status.skill[sk_idx].id = 0;
  1408. }
  1409. }
  1410. }
  1411. // Grant all skills
  1412. pc_grant_allskills(sd, false);
  1413. do {
  1414. uint16 skid = 0;
  1415. flag = 0;
  1416. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++) {
  1417. bool fail = false;
  1418. uint16 sk_idx = skill_get_index(skid);
  1419. if (sd->status.skill[sk_idx].id)
  1420. continue; //Skill already known.
  1421. if (!battle_config.skillfree) {
  1422. uint8 j;
  1423. // Checking required skills
  1424. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1425. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1426. uint16 sk_need_idx = 0;
  1427. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1428. short sk_need = sk_need_id;
  1429. 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)
  1430. sk_need = 0; //Not learned.
  1431. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1432. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1433. else
  1434. sk_need = pc_checkskill(sd,sk_need_id);
  1435. if (sk_need < skill_tree[c][i].need[j].lv) {
  1436. fail = true;
  1437. break;
  1438. }
  1439. }
  1440. }
  1441. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1442. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1443. class_ = pc_class2idx(class_);
  1444. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1445. fail = true; // job level requirement wasn't satisfied
  1446. }
  1447. }
  1448. if (!fail) {
  1449. int inf2 = skill_get_inf2(skid);
  1450. if (!sd->status.skill[sk_idx].lv && (
  1451. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1452. inf2&INF2_WEDDING_SKILL ||
  1453. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1454. ))
  1455. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1456. sd->status.skill[sk_idx].id = skid;
  1457. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1458. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1459. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1460. }
  1461. flag = 1; // skill list has changed, perform another pass
  1462. }
  1463. }
  1464. } while(flag);
  1465. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1466. unsigned short skid = 0;
  1467. /* Taekwon Ranker Bonus Skill Tree
  1468. ============================================
  1469. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1470. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1471. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1472. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1473. uint16 sk_idx = 0;
  1474. if (!(sk_idx = skill_get_index(skid)))
  1475. continue;
  1476. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1477. continue; //Do not include Quest/Wedding skills.
  1478. if( sd->status.skill[sk_idx].id == 0 ) {
  1479. sd->status.skill[sk_idx].id = skid;
  1480. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1481. } else if( skid != NV_BASIC )
  1482. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1483. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1484. }
  1485. }
  1486. }
  1487. //Checks if you can learn a new skill after having leveled up a skill.
  1488. static void pc_check_skilltree(struct map_session_data *sd)
  1489. {
  1490. int i, flag = 0;
  1491. int c = 0;
  1492. if (battle_config.skillfree)
  1493. return; //Function serves no purpose if this is set
  1494. i = pc_calc_skilltree_normalize_job(sd);
  1495. c = pc_mapid2jobid(i, sd->status.sex);
  1496. if (c == -1) { //Unable to normalize job??
  1497. 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);
  1498. return;
  1499. }
  1500. c = pc_class2idx(c);
  1501. do {
  1502. uint16 skid = 0;
  1503. flag = 0;
  1504. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1505. uint16 sk_idx = skill_get_index(skid);
  1506. bool fail = false;
  1507. uint8 j = 0;
  1508. if (sd->status.skill[sk_idx].id) //Already learned
  1509. continue;
  1510. // Checking required skills
  1511. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1512. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1513. uint16 sk_need_idx = 0;
  1514. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1515. short sk_need = sk_need_id;
  1516. 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)
  1517. sk_need = 0; //Not learned.
  1518. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1519. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1520. else
  1521. sk_need = pc_checkskill(sd,sk_need_id);
  1522. if (sk_need < skill_tree[c][i].need[j].lv) {
  1523. fail = true;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. if( fail )
  1529. continue;
  1530. if( sd->status.job_level < skill_tree[c][i].joblv )
  1531. continue;
  1532. j = skill_get_inf2(skid);
  1533. if( !sd->status.skill[sk_idx].lv && (
  1534. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1535. j&INF2_WEDDING_SKILL ||
  1536. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1537. ) )
  1538. continue; //Cannot be learned via normal means.
  1539. sd->status.skill[sk_idx].id = skid;
  1540. flag = 1;
  1541. }
  1542. } while(flag);
  1543. }
  1544. // Make sure all the skills are in the correct condition
  1545. // before persisting to the backend.. [MouseJstr]
  1546. void pc_clean_skilltree(struct map_session_data *sd)
  1547. {
  1548. uint16 i;
  1549. for (i = 0; i < MAX_SKILL; i++){
  1550. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1551. sd->status.skill[i].id = 0;
  1552. sd->status.skill[i].lv = 0;
  1553. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1554. }
  1555. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1556. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1557. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1558. }
  1559. }
  1560. }
  1561. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1562. {
  1563. int skill_point, novice_skills;
  1564. int c = sd->class_;
  1565. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1566. return c;
  1567. skill_point = pc_calc_skillpoint(sd);
  1568. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1569. // limit 1st class and above to novice job levels
  1570. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1571. {
  1572. c = MAPID_NOVICE;
  1573. }
  1574. // limit 2nd class and above to first class job levels (super novices are exempt)
  1575. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1576. {
  1577. // regenerate change_level_2nd
  1578. if (!sd->change_level_2nd)
  1579. {
  1580. if (sd->class_&JOBL_THIRD)
  1581. {
  1582. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1583. if (!sd->change_level_3rd)
  1584. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1585. else
  1586. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1587. - (sd->status.job_level - 1)
  1588. - (sd->change_level_3rd - 1)
  1589. - novice_skills;
  1590. }
  1591. else
  1592. {
  1593. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1594. - (sd->status.job_level - 1)
  1595. - novice_skills;
  1596. }
  1597. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  1598. }
  1599. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1600. {
  1601. c &= MAPID_BASEMASK;
  1602. }
  1603. // limit 3rd class to 2nd class/trans job levels
  1604. else if(sd->class_&JOBL_THIRD)
  1605. {
  1606. // regenerate change_level_3rd
  1607. if (!sd->change_level_3rd)
  1608. {
  1609. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1610. - (sd->status.job_level - 1)
  1611. - (sd->change_level_2nd - 1)
  1612. - novice_skills;
  1613. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  1614. }
  1615. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1616. c &= MAPID_UPPERMASK;
  1617. }
  1618. }
  1619. // restore non-limiting flags
  1620. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1621. return c;
  1622. }
  1623. /*==========================================
  1624. * Updates the weight status
  1625. *------------------------------------------
  1626. * 1: overweight 50%
  1627. * 2: overweight 90%
  1628. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1629. */
  1630. void pc_updateweightstatus(struct map_session_data *sd)
  1631. {
  1632. int old_overweight;
  1633. int new_overweight;
  1634. nullpo_retv(sd);
  1635. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1636. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1637. if( old_overweight == new_overweight )
  1638. return; // no change
  1639. // stop old status change
  1640. if( old_overweight == 1 )
  1641. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1642. else if( old_overweight == 2 )
  1643. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1644. // start new status change
  1645. if( new_overweight == 1 )
  1646. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1647. else if( new_overweight == 2 )
  1648. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1649. // update overweight status
  1650. sd->regen.state.overweight = new_overweight;
  1651. }
  1652. int pc_disguise(struct map_session_data *sd, int class_)
  1653. {
  1654. if (!class_ && !sd->disguise)
  1655. return 0;
  1656. if (class_ && sd->disguise == class_)
  1657. return 0;
  1658. if(sd->sc.option&OPTION_INVISIBLE)
  1659. { //Character is invisible. Stealth class-change. [Skotlex]
  1660. sd->disguise = class_; //viewdata is set on uncloaking.
  1661. return 2;
  1662. }
  1663. if (sd->bl.prev != NULL) {
  1664. pc_stop_walking(sd, 0);
  1665. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1666. }
  1667. if (!class_) {
  1668. sd->disguise = 0;
  1669. class_ = sd->status.class_;
  1670. } else
  1671. sd->disguise=class_;
  1672. status_set_viewdata(&sd->bl, class_);
  1673. clif_changeoption(&sd->bl);
  1674. if (sd->bl.prev != NULL) {
  1675. clif_spawn(&sd->bl);
  1676. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1677. { //It seems the cart info is lost on undisguise.
  1678. clif_cartlist(sd);
  1679. clif_updatestatus(sd,SP_CARTINFO);
  1680. }
  1681. if (sd->chatID) {
  1682. struct chat_data* cd;
  1683. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1684. clif_dispchat(cd,0);
  1685. }
  1686. }
  1687. return 1;
  1688. }
  1689. /// Show error message
  1690. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1691. /// Check for valid Element, break & show error message if invalid Element
  1692. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1693. /// Check for valid Race, break & show error message if invalid Race
  1694. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1695. /// Check for valid Race2, break & show error message if invalid Race2
  1696. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1697. /// Check for valid Class, break & show error message if invalid Class
  1698. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1699. /// Check for valid Size, break & show error message if invalid Size
  1700. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1701. /// Check for valid SC, break & show error message if invalid SC
  1702. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1703. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1704. {
  1705. uint8 i;
  1706. if( !rate )
  1707. return;
  1708. for( i = 0; i < max && spell[i].id; i++ )
  1709. {
  1710. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1711. {
  1712. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1713. return;
  1714. rate += spell[i].rate;
  1715. break;
  1716. }
  1717. }
  1718. if (i == max) {
  1719. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1720. return;
  1721. }
  1722. spell[i].id = id;
  1723. spell[i].lv = lv;
  1724. spell[i].rate = rate;
  1725. //Auto-update flag value.
  1726. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1727. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1728. if (!(flag&BF_SKILLMASK)) {
  1729. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1730. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1731. }
  1732. spell[i].flag|= flag;
  1733. spell[i].card_id = card_id;
  1734. }
  1735. 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)
  1736. {
  1737. uint8 i;
  1738. if( !rate )
  1739. return;
  1740. for( i = 0; i < max && spell[i].id; i++ )
  1741. {
  1742. ; // each autospell works independently
  1743. }
  1744. if( i == max )
  1745. {
  1746. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1747. return;
  1748. }
  1749. spell[i].flag = src_skill;
  1750. spell[i].id = id;
  1751. spell[i].lv = lv;
  1752. spell[i].rate = rate;
  1753. spell[i].card_id = card_id;
  1754. return;
  1755. }
  1756. /**
  1757. * Add inflict effect bonus for player while attacking/atatcked
  1758. * @param effect Effect array
  1759. * @param pmax Max array
  1760. * @param sc SC/Effect type
  1761. * @param rate Success chance
  1762. * @param arrow_rate success chance if bonus comes from arrow-type item
  1763. * @param flag Target flag
  1764. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1765. **/
  1766. static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  1767. {
  1768. uint16 i;
  1769. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1770. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1771. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1772. flag |= ATF_TARGET; //Default target: enemy.
  1773. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1774. flag |= ATF_WEAPON; //Default type: weapon.
  1775. if (!duration)
  1776. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1777. for (i = 0; i < pmax && effect[i].flag; i++) {
  1778. if (effect[i].sc == sc && effect[i].flag == flag) {
  1779. effect[i].rate += rate;
  1780. effect[i].arrow_rate += arrow_rate;
  1781. effect[i].duration = umax(effect[i].duration, duration);
  1782. return;
  1783. }
  1784. }
  1785. if (i == pmax) {
  1786. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1787. return;
  1788. }
  1789. effect[i].sc = sc;
  1790. effect[i].rate = rate;
  1791. effect[i].arrow_rate = arrow_rate;
  1792. effect[i].flag = flag;
  1793. effect[i].duration = duration;
  1794. }
  1795. /**
  1796. * Add inflict effect bonus for player while attacking using skill
  1797. * @param effect Effect array
  1798. * @param pmax Max array
  1799. * @param sc SC/Effect type
  1800. * @param rate Success chance
  1801. * @param flag Target flag
  1802. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1803. **/
  1804. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  1805. {
  1806. uint8 i;
  1807. if (!duration)
  1808. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1809. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1810. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1811. effect[i].rate += rate;
  1812. effect[i].duration = umax(effect[i].duration, duration);
  1813. return;
  1814. }
  1815. }
  1816. if( i == pmax ) {
  1817. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1818. return;
  1819. }
  1820. effect[i].sc = sc;
  1821. effect[i].rate = rate;
  1822. effect[i].skill_id = skill_id;
  1823. effect[i].target = target;
  1824. effect[i].duration = duration;
  1825. }
  1826. /** Adjust/add drop rate modifier for player
  1827. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1828. * @param max: Max bonus can be received
  1829. * @param nameid: item id that will be dropped
  1830. * @param group: group id
  1831. * @param class_: target class
  1832. * @param race: target race. if < 0, means monster_id
  1833. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1834. */
  1835. 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)
  1836. {
  1837. uint8 i;
  1838. if (!nameid && !group) {
  1839. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1840. return;
  1841. }
  1842. if (nameid && !itemdb_exists(nameid)) {
  1843. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1844. return;
  1845. }
  1846. if (group && !itemdb_group_exists(group)) {
  1847. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1848. return;
  1849. }
  1850. //Apply config rate adjustment settings.
  1851. if (rate >= 0) { //Absolute drop.
  1852. if (battle_config.item_rate_adddrop != 100)
  1853. rate = rate*battle_config.item_rate_adddrop/100;
  1854. if (rate < battle_config.item_drop_adddrop_min)
  1855. rate = battle_config.item_drop_adddrop_min;
  1856. else if (rate > battle_config.item_drop_adddrop_max)
  1857. rate = battle_config.item_drop_adddrop_max;
  1858. } else { //Relative drop, max/min limits are applied at drop time.
  1859. if (battle_config.item_rate_adddrop != 100)
  1860. rate = rate*battle_config.item_rate_adddrop/100;
  1861. if (rate > -1)
  1862. rate = -1;
  1863. }
  1864. //Find match entry, and adjust the rate only
  1865. for (i = 0; i < max; i++) {
  1866. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1867. continue;
  1868. if (drop[i].nameid == nameid &&
  1869. drop[i].group == group &&
  1870. drop[i].race == race &&
  1871. drop[i].class_ == class_
  1872. )
  1873. {
  1874. //Adjust the rate if it has same classification
  1875. if ((rate < 0 && drop[i].rate < 0) ||
  1876. (rate > 0 && drop[i].rate > 0))
  1877. {
  1878. drop[i].rate += rate;
  1879. return;
  1880. }
  1881. }
  1882. }
  1883. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1884. if (i >= max) {
  1885. 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);
  1886. return;
  1887. }
  1888. drop[i].nameid = nameid;
  1889. drop[i].group = group;
  1890. drop[i].race = race;
  1891. drop[i].class_ = class_;
  1892. drop[i].rate = rate;
  1893. }
  1894. 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)
  1895. {
  1896. int i;
  1897. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1898. if( i == max )
  1899. {
  1900. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1901. return false;
  1902. }
  1903. if( !onskill )
  1904. {
  1905. if( !(flag&BF_RANGEMASK) )
  1906. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1907. if( !(flag&BF_WEAPONMASK) )
  1908. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1909. if( !(flag&BF_SKILLMASK) )
  1910. {
  1911. if( flag&(BF_MAGIC|BF_MISC) )
  1912. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1913. if( flag&BF_WEAPON )
  1914. flag|=BF_NORMAL|BF_SKILL;
  1915. }
  1916. }
  1917. bonus[i].rate = rate;
  1918. bonus[i].duration = dur;
  1919. bonus[i].active = INVALID_TIMER;
  1920. bonus[i].atk_type = flag;
  1921. bonus[i].pos = pos;
  1922. bonus[i].bonus_script = aStrdup(script);
  1923. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1924. return true;
  1925. }
  1926. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1927. {
  1928. int i;
  1929. if (!sd)
  1930. return;
  1931. for( i = 0; i < max; i++ )
  1932. {
  1933. if( autobonus[i].active != INVALID_TIMER )
  1934. {
  1935. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1936. {
  1937. if( autobonus[i].bonus_script )
  1938. {
  1939. int j;
  1940. unsigned int equip_pos_idx = 0;
  1941. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1942. for(j = 0; j < EQI_MAX; j++) {
  1943. if(sd->equip_index[j] >= 0)
  1944. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1945. }
  1946. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  1947. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  1948. }
  1949. continue;
  1950. }
  1951. else
  1952. { // Logout / Unequipped an item with an activated bonus
  1953. delete_timer(autobonus[i].active,pc_endautobonus);
  1954. autobonus[i].active = INVALID_TIMER;
  1955. }
  1956. }
  1957. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1958. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1959. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1960. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1961. autobonus[i].active = INVALID_TIMER;
  1962. }
  1963. }
  1964. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1965. {
  1966. if (!sd || !autobonus)
  1967. return;
  1968. if( autobonus->other_script )
  1969. {
  1970. int j;
  1971. unsigned int equip_pos_idx = 0;
  1972. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1973. for(j = 0; j < EQI_MAX; j++) {
  1974. if(sd->equip_index[j] >= 0)
  1975. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1976. }
  1977. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  1978. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  1979. }
  1980. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1981. sd->state.autobonus |= autobonus->pos;
  1982. status_calc_pc(sd,SCO_FORCE);
  1983. }
  1984. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1985. {
  1986. struct map_session_data *sd = map_id2sd(id);
  1987. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1988. nullpo_ret(sd);
  1989. nullpo_ret(autobonus);
  1990. autobonus->active = INVALID_TIMER;
  1991. sd->state.autobonus &= ~autobonus->pos;
  1992. status_calc_pc(sd,SCO_FORCE);
  1993. return 0;
  1994. }
  1995. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1996. {
  1997. uint8 i;
  1998. struct weapon_data* wd;
  1999. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2000. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2001. if (i == MAX_PC_BONUS)
  2002. {
  2003. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2004. return;
  2005. }
  2006. if (!(flag&BF_RANGEMASK))
  2007. flag |= BF_SHORT|BF_LONG;
  2008. if (!(flag&BF_WEAPONMASK))
  2009. flag |= BF_WEAPON;
  2010. if (!(flag&BF_SKILLMASK))
  2011. {
  2012. if (flag&(BF_MAGIC|BF_MISC))
  2013. flag |= BF_SKILL;
  2014. if (flag&BF_WEAPON)
  2015. flag |= BF_NORMAL|BF_SKILL;
  2016. }
  2017. wd->addele2[i].ele = ele;
  2018. wd->addele2[i].rate = rate;
  2019. wd->addele2[i].flag = flag;
  2020. }
  2021. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2022. {
  2023. uint8 i;
  2024. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2025. if (i == MAX_PC_BONUS)
  2026. {
  2027. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2028. return;
  2029. }
  2030. if (!(flag&BF_RANGEMASK))
  2031. flag |= BF_SHORT|BF_LONG;
  2032. if (!(flag&BF_WEAPONMASK))
  2033. flag |= BF_WEAPON;
  2034. if (!(flag&BF_SKILLMASK))
  2035. {
  2036. if (flag&(BF_MAGIC|BF_MISC))
  2037. flag |= BF_SKILL;
  2038. if (flag&BF_WEAPON)
  2039. flag |= BF_NORMAL|BF_SKILL;
  2040. }
  2041. sd->subele2[i].ele = ele;
  2042. sd->subele2[i].rate = rate;
  2043. sd->subele2[i].flag = flag;
  2044. }
  2045. /** Add item group heal rate bonus to player
  2046. * @param sd Player
  2047. * @param group_id Item Group ID
  2048. * @param rate
  2049. * @author Cydh
  2050. */
  2051. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2052. struct s_pc_itemgrouphealrate *entry;
  2053. uint8 i;
  2054. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2055. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2056. break;
  2057. }
  2058. if (i != sd->itemgrouphealrate_count) {
  2059. sd->itemgrouphealrate[i]->rate += rate;
  2060. return;
  2061. }
  2062. if (i >= UINT8_MAX) {
  2063. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2064. return;
  2065. }
  2066. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2067. entry->group_id = group_id;
  2068. entry->rate = rate;
  2069. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2070. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2071. }
  2072. /** Clear item group heal rate from player
  2073. * @param sd Player
  2074. * @author Cydh
  2075. */
  2076. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2077. if (!sd || !sd->itemgrouphealrate_count)
  2078. return;
  2079. else {
  2080. uint8 i;
  2081. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2082. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2083. sd->itemgrouphealrate_count = 0;
  2084. aFree(sd->itemgrouphealrate);
  2085. sd->itemgrouphealrate = NULL;
  2086. }
  2087. }
  2088. /*==========================================
  2089. * Add a bonus(type) to player sd
  2090. * format: bonus bBonusName,val;
  2091. * @param sd
  2092. * @param type Bonus type used by bBonusName
  2093. * @param val Value that usually for rate or fixed value
  2094. *------------------------------------------*/
  2095. void pc_bonus(struct map_session_data *sd,int type,int val)
  2096. {
  2097. struct status_data *status;
  2098. int bonus;
  2099. nullpo_retv(sd);
  2100. status = &sd->base_status;
  2101. switch(type){
  2102. case SP_STR:
  2103. case SP_AGI:
  2104. case SP_VIT:
  2105. case SP_INT:
  2106. case SP_DEX:
  2107. case SP_LUK:
  2108. if(sd->state.lr_flag != 2)
  2109. sd->param_bonus[type-SP_STR]+=val;
  2110. break;
  2111. case SP_ATK1:
  2112. if(!sd->state.lr_flag) {
  2113. bonus = status->rhw.atk + val;
  2114. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2115. }
  2116. else if(sd->state.lr_flag == 1) {
  2117. bonus = status->lhw.atk + val;
  2118. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2119. }
  2120. break;
  2121. case SP_ATK2:
  2122. if(!sd->state.lr_flag) {
  2123. bonus = status->rhw.atk2 + val;
  2124. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2125. }
  2126. else if(sd->state.lr_flag == 1) {
  2127. bonus = status->lhw.atk2 + val;
  2128. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2129. }
  2130. break;
  2131. case SP_BASE_ATK:
  2132. if(sd->state.lr_flag != 2) {
  2133. #ifdef RENEWAL
  2134. sd->bonus.eatk += val;
  2135. #else
  2136. bonus = status->batk + val;
  2137. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2138. #endif
  2139. }
  2140. break;
  2141. case SP_DEF1:
  2142. if(sd->state.lr_flag != 2) {
  2143. bonus = status->def + val;
  2144. #ifdef RENEWAL
  2145. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2146. #else
  2147. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2148. #endif
  2149. }
  2150. break;
  2151. case SP_DEF2:
  2152. if(sd->state.lr_flag != 2) {
  2153. bonus = status->def2 + val;
  2154. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2155. }
  2156. break;
  2157. case SP_MDEF1:
  2158. if(sd->state.lr_flag != 2) {
  2159. bonus = status->mdef + val;
  2160. #ifdef RENEWAL
  2161. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2162. #else
  2163. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2164. #endif
  2165. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2166. sd->bonus.shieldmdef += bonus;
  2167. }
  2168. }
  2169. break;
  2170. case SP_MDEF2:
  2171. if(sd->state.lr_flag != 2) {
  2172. bonus = status->mdef2 + val;
  2173. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2174. }
  2175. break;
  2176. case SP_HIT:
  2177. if(sd->state.lr_flag != 2) {
  2178. bonus = status->hit + val;
  2179. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2180. } else
  2181. sd->bonus.arrow_hit+=val;
  2182. break;
  2183. case SP_FLEE1:
  2184. if(sd->state.lr_flag != 2) {
  2185. bonus = status->flee + val;
  2186. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2187. }
  2188. break;
  2189. case SP_FLEE2:
  2190. if(sd->state.lr_flag != 2) {
  2191. bonus = status->flee2 + val*10;
  2192. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2193. }
  2194. break;
  2195. case SP_CRITICAL:
  2196. if(sd->state.lr_flag != 2) {
  2197. bonus = status->cri + val*10;
  2198. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2199. } else
  2200. sd->bonus.arrow_cri += val*10;
  2201. break;
  2202. case SP_ATKELE:
  2203. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2204. switch (sd->state.lr_flag)
  2205. {
  2206. case 2:
  2207. switch (sd->status.weapon) {
  2208. case W_BOW:
  2209. case W_REVOLVER:
  2210. case W_RIFLE:
  2211. case W_GATLING:
  2212. case W_SHOTGUN:
  2213. case W_GRENADE:
  2214. //Become weapon element.
  2215. status->rhw.ele=val;
  2216. break;
  2217. default: //Become arrow element.
  2218. sd->bonus.arrow_ele=val;
  2219. break;
  2220. }
  2221. break;
  2222. case 1:
  2223. status->lhw.ele=val;
  2224. break;
  2225. default:
  2226. status->rhw.ele=val;
  2227. break;
  2228. }
  2229. break;
  2230. case SP_DEFELE:
  2231. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2232. if(sd->state.lr_flag != 2)
  2233. status->def_ele=val;
  2234. break;
  2235. case SP_MAXHP:
  2236. if(sd->state.lr_flag == 2)
  2237. break;
  2238. sd->bonus.hp += val;
  2239. break;
  2240. case SP_MAXSP:
  2241. if(sd->state.lr_flag == 2)
  2242. break;
  2243. sd->bonus.sp += val;
  2244. break;
  2245. case SP_MAXHPRATE:
  2246. if(sd->state.lr_flag != 2)
  2247. sd->hprate+=val;
  2248. break;
  2249. case SP_MAXSPRATE:
  2250. if(sd->state.lr_flag != 2)
  2251. sd->sprate+=val;
  2252. break;
  2253. case SP_SPRATE:
  2254. if(sd->state.lr_flag != 2)
  2255. sd->dsprate+=val;
  2256. break;
  2257. case SP_ATTACKRANGE:
  2258. switch (sd->state.lr_flag) {
  2259. case 2:
  2260. switch (sd->status.weapon) {
  2261. case W_BOW:
  2262. case W_REVOLVER:
  2263. case W_RIFLE:
  2264. case W_GATLING:
  2265. case W_SHOTGUN:
  2266. case W_GRENADE:
  2267. status->rhw.range += val;
  2268. }
  2269. break;
  2270. case 1:
  2271. status->lhw.range += val;
  2272. break;
  2273. default:
  2274. status->rhw.range += val;
  2275. break;
  2276. }
  2277. break;
  2278. case SP_SPEED_RATE: //Non stackable increase
  2279. if(sd->state.lr_flag != 2)
  2280. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2281. break;
  2282. case SP_SPEED_ADDRATE: //Stackable increase
  2283. if(sd->state.lr_flag != 2)
  2284. sd->bonus.speed_add_rate -= val;
  2285. break;
  2286. case SP_ASPD: //Raw increase
  2287. if(sd->state.lr_flag != 2)
  2288. sd->bonus.aspd_add -= 10*val;
  2289. break;
  2290. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2291. if(sd->state.lr_flag != 2)
  2292. #ifndef RENEWAL_ASPD
  2293. status->aspd_rate -= 10*val;
  2294. #else
  2295. status->aspd_rate2 += val;
  2296. #endif
  2297. break;
  2298. case SP_HP_RECOV_RATE:
  2299. if(sd->state.lr_flag != 2)
  2300. sd->hprecov_rate += val;
  2301. break;
  2302. case SP_SP_RECOV_RATE:
  2303. if(sd->state.lr_flag != 2)
  2304. sd->sprecov_rate += val;
  2305. break;
  2306. case SP_CRITICAL_DEF:
  2307. if(sd->state.lr_flag != 2)
  2308. sd->bonus.critical_def += val;
  2309. break;
  2310. case SP_NEAR_ATK_DEF:
  2311. if(sd->state.lr_flag != 2)
  2312. sd->bonus.near_attack_def_rate += val;
  2313. break;
  2314. case SP_LONG_ATK_DEF:
  2315. if(sd->state.lr_flag != 2)
  2316. sd->bonus.long_attack_def_rate += val;
  2317. break;
  2318. case SP_DOUBLE_RATE:
  2319. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2320. sd->bonus.double_rate = val;
  2321. break;
  2322. case SP_DOUBLE_ADD_RATE:
  2323. if(sd->state.lr_flag == 0)
  2324. sd->bonus.double_add_rate += val;
  2325. break;
  2326. case SP_MATK_RATE:
  2327. if(sd->state.lr_flag != 2)
  2328. sd->matk_rate += val;
  2329. break;
  2330. case SP_IGNORE_DEF_ELE:
  2331. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2332. if(!sd->state.lr_flag)
  2333. sd->right_weapon.ignore_def_ele |= 1<<val;
  2334. else if(sd->state.lr_flag == 1)
  2335. sd->left_weapon.ignore_def_ele |= 1<<val;
  2336. break;
  2337. case SP_IGNORE_DEF_RACE:
  2338. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2339. if(!sd->state.lr_flag)
  2340. sd->right_weapon.ignore_def_race |= 1<<val;
  2341. else if(sd->state.lr_flag == 1)
  2342. sd->left_weapon.ignore_def_race |= 1<<val;
  2343. break;
  2344. case SP_IGNORE_DEF_CLASS:
  2345. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2346. if(!sd->state.lr_flag)
  2347. sd->right_weapon.ignore_def_class |= 1<<val;
  2348. else if(sd->state.lr_flag == 1)
  2349. sd->left_weapon.ignore_def_class |= 1<<val;
  2350. break;
  2351. case SP_ATK_RATE:
  2352. if(sd->state.lr_flag != 2)
  2353. sd->bonus.atk_rate += val;
  2354. break;
  2355. case SP_MAGIC_ATK_DEF:
  2356. if(sd->state.lr_flag != 2)
  2357. sd->bonus.magic_def_rate += val;
  2358. break;
  2359. case SP_MISC_ATK_DEF:
  2360. if(sd->state.lr_flag != 2)
  2361. sd->bonus.misc_def_rate += val;
  2362. break;
  2363. case SP_IGNORE_MDEF_ELE:
  2364. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2365. if(sd->state.lr_flag != 2)
  2366. sd->bonus.ignore_mdef_ele |= 1<<val;
  2367. break;
  2368. case SP_IGNORE_MDEF_RACE:
  2369. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2370. if(sd->state.lr_flag != 2)
  2371. sd->bonus.ignore_mdef_race |= 1<<val;
  2372. break;
  2373. case SP_PERFECT_HIT_RATE:
  2374. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2375. sd->bonus.perfect_hit = val;
  2376. break;
  2377. case SP_PERFECT_HIT_ADD_RATE:
  2378. if(sd->state.lr_flag != 2)
  2379. sd->bonus.perfect_hit_add += val;
  2380. break;
  2381. case SP_CRITICAL_RATE:
  2382. if(sd->state.lr_flag != 2)
  2383. sd->critical_rate+=val;
  2384. break;
  2385. case SP_DEF_RATIO_ATK_ELE:
  2386. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2387. if(!sd->state.lr_flag)
  2388. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2389. else if(sd->state.lr_flag == 1)
  2390. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2391. break;
  2392. case SP_DEF_RATIO_ATK_RACE:
  2393. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2394. if(!sd->state.lr_flag)
  2395. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2396. else if(sd->state.lr_flag == 1)
  2397. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2398. break;
  2399. case SP_DEF_RATIO_ATK_CLASS:
  2400. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2401. if(!sd->state.lr_flag)
  2402. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2403. else if(sd->state.lr_flag == 1)
  2404. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2405. break;
  2406. case SP_HIT_RATE:
  2407. if(sd->state.lr_flag != 2)
  2408. sd->hit_rate += val;
  2409. break;
  2410. case SP_FLEE_RATE:
  2411. if(sd->state.lr_flag != 2)
  2412. sd->flee_rate += val;
  2413. break;
  2414. case SP_FLEE2_RATE:
  2415. if(sd->state.lr_flag != 2)
  2416. sd->flee2_rate += val;
  2417. break;
  2418. case SP_DEF_RATE:
  2419. if(sd->state.lr_flag != 2)
  2420. sd->def_rate += val;
  2421. break;
  2422. case SP_DEF2_RATE:
  2423. if(sd->state.lr_flag != 2)
  2424. sd->def2_rate += val;
  2425. break;
  2426. case SP_MDEF_RATE:
  2427. if(sd->state.lr_flag != 2)
  2428. sd->mdef_rate += val;
  2429. break;
  2430. case SP_MDEF2_RATE:
  2431. if(sd->state.lr_flag != 2)
  2432. sd->mdef2_rate += val;
  2433. break;
  2434. case SP_RESTART_FULL_RECOVER:
  2435. if(sd->state.lr_flag != 2)
  2436. sd->special_state.restart_full_recover = 1;
  2437. break;
  2438. case SP_NO_CASTCANCEL:
  2439. if(sd->state.lr_flag != 2)
  2440. sd->special_state.no_castcancel = 1;
  2441. break;
  2442. case SP_NO_CASTCANCEL2:
  2443. if(sd->state.lr_flag != 2)
  2444. sd->special_state.no_castcancel2 = 1;
  2445. break;
  2446. case SP_NO_SIZEFIX:
  2447. if(sd->state.lr_flag != 2)
  2448. sd->special_state.no_sizefix = 1;
  2449. break;
  2450. case SP_NO_MAGIC_DAMAGE:
  2451. if(sd->state.lr_flag == 2)
  2452. break;
  2453. val+= sd->special_state.no_magic_damage;
  2454. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2455. break;
  2456. case SP_NO_WEAPON_DAMAGE:
  2457. if(sd->state.lr_flag == 2)
  2458. break;
  2459. val+= sd->special_state.no_weapon_damage;
  2460. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2461. break;
  2462. case SP_NO_MISC_DAMAGE:
  2463. if(sd->state.lr_flag == 2)
  2464. break;
  2465. val+= sd->special_state.no_misc_damage;
  2466. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2467. break;
  2468. case SP_NO_GEMSTONE:
  2469. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2470. sd->special_state.no_gemstone = 1;
  2471. break;
  2472. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2473. if(sd->state.lr_flag != 2) {
  2474. sd->special_state.intravision = 1;
  2475. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2476. }
  2477. break;
  2478. case SP_NO_KNOCKBACK:
  2479. if(sd->state.lr_flag != 2)
  2480. sd->special_state.no_knockback = 1;
  2481. break;
  2482. case SP_SPLASH_RANGE:
  2483. if(sd->bonus.splash_range < val)
  2484. sd->bonus.splash_range = val;
  2485. break;
  2486. case SP_SPLASH_ADD_RANGE:
  2487. sd->bonus.splash_add_range += val;
  2488. break;
  2489. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2490. if(sd->state.lr_flag != 2)
  2491. sd->bonus.short_weapon_damage_return += val;
  2492. break;
  2493. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2494. if(sd->state.lr_flag != 2)
  2495. sd->bonus.long_weapon_damage_return += val;
  2496. break;
  2497. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2498. if(sd->state.lr_flag != 2)
  2499. sd->bonus.magic_damage_return += val;
  2500. break;
  2501. case SP_ALL_STATS: // [Valaris]
  2502. if(sd->state.lr_flag!=2) {
  2503. sd->param_bonus[SP_STR-SP_STR]+=val;
  2504. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2505. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2506. sd->param_bonus[SP_INT-SP_STR]+=val;
  2507. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2508. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2509. }
  2510. break;
  2511. case SP_AGI_VIT: // [Valaris]
  2512. if(sd->state.lr_flag!=2) {
  2513. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2514. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2515. }
  2516. break;
  2517. case SP_AGI_DEX_STR: // [Valaris]
  2518. if(sd->state.lr_flag!=2) {
  2519. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2520. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2521. sd->param_bonus[SP_STR-SP_STR]+=val;
  2522. }
  2523. break;
  2524. case SP_PERFECT_HIDE: // [Valaris]
  2525. if(sd->state.lr_flag!=2)
  2526. sd->special_state.perfect_hiding=1;
  2527. break;
  2528. case SP_UNBREAKABLE:
  2529. if(sd->state.lr_flag!=2)
  2530. sd->bonus.unbreakable += val;
  2531. break;
  2532. case SP_UNBREAKABLE_WEAPON:
  2533. if(sd->state.lr_flag != 2)
  2534. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2535. break;
  2536. case SP_UNBREAKABLE_ARMOR:
  2537. if(sd->state.lr_flag != 2)
  2538. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2539. break;
  2540. case SP_UNBREAKABLE_HELM:
  2541. if(sd->state.lr_flag != 2)
  2542. sd->bonus.unbreakable_equip |= EQP_HELM;
  2543. break;
  2544. case SP_UNBREAKABLE_SHIELD:
  2545. if(sd->state.lr_flag != 2)
  2546. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2547. break;
  2548. case SP_UNBREAKABLE_GARMENT:
  2549. if(sd->state.lr_flag != 2)
  2550. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2551. break;
  2552. case SP_UNBREAKABLE_SHOES:
  2553. if(sd->state.lr_flag != 2)
  2554. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2555. break;
  2556. case SP_CLASSCHANGE: // [Valaris]
  2557. if(sd->state.lr_flag !=2)
  2558. sd->bonus.classchange=val;
  2559. break;
  2560. case SP_LONG_ATK_RATE:
  2561. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2562. sd->bonus.long_attack_atk_rate+=val;
  2563. break;
  2564. case SP_BREAK_WEAPON_RATE:
  2565. if(sd->state.lr_flag != 2)
  2566. sd->bonus.break_weapon_rate+=val;
  2567. break;
  2568. case SP_BREAK_ARMOR_RATE:
  2569. if(sd->state.lr_flag != 2)
  2570. sd->bonus.break_armor_rate+=val;
  2571. break;
  2572. case SP_ADD_STEAL_RATE:
  2573. if(sd->state.lr_flag != 2)
  2574. sd->bonus.add_steal_rate+=val;
  2575. break;
  2576. case SP_DELAYRATE:
  2577. if(sd->state.lr_flag != 2)
  2578. sd->delayrate+=val;
  2579. break;
  2580. case SP_CRIT_ATK_RATE:
  2581. if(sd->state.lr_flag != 2)
  2582. sd->bonus.crit_atk_rate += val;
  2583. break;
  2584. case SP_NO_REGEN:
  2585. if(sd->state.lr_flag != 2)
  2586. sd->regen.state.block|=val;
  2587. break;
  2588. case SP_UNSTRIPABLE_WEAPON:
  2589. if(sd->state.lr_flag != 2)
  2590. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2591. break;
  2592. case SP_UNSTRIPABLE:
  2593. case SP_UNSTRIPABLE_ARMOR:
  2594. if(sd->state.lr_flag != 2)
  2595. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2596. break;
  2597. case SP_UNSTRIPABLE_HELM:
  2598. if(sd->state.lr_flag != 2)
  2599. sd->bonus.unstripable_equip |= EQP_HELM;
  2600. break;
  2601. case SP_UNSTRIPABLE_SHIELD:
  2602. if(sd->state.lr_flag != 2)
  2603. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2604. break;
  2605. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2606. if(!sd->state.lr_flag) {
  2607. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2608. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2609. } else if(sd->state.lr_flag == 1) {
  2610. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2611. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2612. }
  2613. break;
  2614. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2615. if(!sd->state.lr_flag) {
  2616. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2617. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2618. } else if(sd->state.lr_flag == 1) {
  2619. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2620. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2621. }
  2622. break;
  2623. case SP_SP_GAIN_VALUE:
  2624. if(!sd->state.lr_flag)
  2625. sd->bonus.sp_gain_value += val;
  2626. break;
  2627. case SP_HP_GAIN_VALUE:
  2628. if(!sd->state.lr_flag)
  2629. sd->bonus.hp_gain_value += val;
  2630. break;
  2631. case SP_MAGIC_SP_GAIN_VALUE:
  2632. if(!sd->state.lr_flag)
  2633. sd->bonus.magic_sp_gain_value += val;
  2634. break;
  2635. case SP_MAGIC_HP_GAIN_VALUE:
  2636. if(!sd->state.lr_flag)
  2637. sd->bonus.magic_hp_gain_value += val;
  2638. break;
  2639. case SP_ADD_HEAL_RATE:
  2640. if(sd->state.lr_flag != 2)
  2641. sd->bonus.add_heal_rate += val;
  2642. break;
  2643. case SP_ADD_HEAL2_RATE:
  2644. if(sd->state.lr_flag != 2)
  2645. sd->bonus.add_heal2_rate += val;
  2646. break;
  2647. case SP_ADD_ITEM_HEAL_RATE:
  2648. if(sd->state.lr_flag != 2)
  2649. sd->bonus.itemhealrate2 += val;
  2650. break;
  2651. case SP_EMATK:
  2652. if(sd->state.lr_flag != 2)
  2653. sd->bonus.ematk += val;
  2654. break;
  2655. #ifdef RENEWAL_CAST
  2656. case SP_FIXCASTRATE:
  2657. if(sd->state.lr_flag != 2)
  2658. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2659. break;
  2660. case SP_ADD_FIXEDCAST:
  2661. if(sd->state.lr_flag != 2)
  2662. sd->bonus.add_fixcast += val;
  2663. break;
  2664. case SP_CASTRATE:
  2665. case SP_VARCASTRATE:
  2666. if(sd->state.lr_flag != 2)
  2667. sd->bonus.varcastrate -= val;
  2668. break;
  2669. case SP_ADD_VARIABLECAST:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.add_varcast += val;
  2672. break;
  2673. #else
  2674. case SP_ADD_FIXEDCAST:
  2675. case SP_FIXCASTRATE:
  2676. case SP_ADD_VARIABLECAST:
  2677. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2678. break;
  2679. case SP_VARCASTRATE:
  2680. case SP_CASTRATE:
  2681. if(sd->state.lr_flag != 2)
  2682. sd->castrate += val;
  2683. break;
  2684. #endif
  2685. case SP_ADDMAXWEIGHT:
  2686. if (sd->state.lr_flag != 2)
  2687. sd->max_weight += val;
  2688. break;
  2689. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2690. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2691. break;
  2692. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2693. if (sd->state.lr_flag != 2)
  2694. sd->bonus.critical_rangeatk += val*10;
  2695. else
  2696. sd->bonus.arrow_cri += val*10;
  2697. break;
  2698. default:
  2699. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2700. break;
  2701. }
  2702. }
  2703. /*==========================================
  2704. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2705. * format: bonus2 bBonusName,type2,val;
  2706. * @param sd
  2707. * @param type Bonus type used by bBonusName
  2708. * @param type2
  2709. * @param val Value that usually for rate or fixed value
  2710. *------------------------------------------*/
  2711. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2712. {
  2713. int i;
  2714. nullpo_retv(sd);
  2715. switch(type){
  2716. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2717. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2718. if(!sd->state.lr_flag)
  2719. sd->right_weapon.addele[type2]+=val;
  2720. else if(sd->state.lr_flag == 1)
  2721. sd->left_weapon.addele[type2]+=val;
  2722. else if(sd->state.lr_flag == 2)
  2723. sd->arrow_addele[type2]+=val;
  2724. break;
  2725. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2726. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2727. if(!sd->state.lr_flag)
  2728. sd->right_weapon.addrace[type2]+=val;
  2729. else if(sd->state.lr_flag == 1)
  2730. sd->left_weapon.addrace[type2]+=val;
  2731. else if(sd->state.lr_flag == 2)
  2732. sd->arrow_addrace[type2]+=val;
  2733. break;
  2734. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2735. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2736. if(!sd->state.lr_flag)
  2737. sd->right_weapon.addclass[type2]+=val;
  2738. else if(sd->state.lr_flag == 1)
  2739. sd->left_weapon.addclass[type2]+=val;
  2740. else if(sd->state.lr_flag == 2)
  2741. sd->arrow_addclass[type2]+=val;
  2742. break;
  2743. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2744. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2745. if(!sd->state.lr_flag)
  2746. sd->right_weapon.addsize[type2]+=val;
  2747. else if(sd->state.lr_flag == 1)
  2748. sd->left_weapon.addsize[type2]+=val;
  2749. else if(sd->state.lr_flag == 2)
  2750. sd->arrow_addsize[type2]+=val;
  2751. break;
  2752. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2753. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2754. if(sd->state.lr_flag != 2)
  2755. sd->subele_script[type2] += val;
  2756. break;
  2757. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2758. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2759. if(sd->state.lr_flag != 2)
  2760. sd->subrace[type2]+=val;
  2761. break;
  2762. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2763. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2764. if(sd->state.lr_flag != 2)
  2765. sd->subclass[type2]+=val;
  2766. break;
  2767. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2768. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2769. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2770. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2771. break;
  2772. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2773. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2774. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2775. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2776. break;
  2777. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2778. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2779. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2780. break;
  2781. }
  2782. if(sd->state.lr_flag == 2)
  2783. break;
  2784. i = sd->reseff[type2]+val;
  2785. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2786. break;
  2787. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2788. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2789. if(sd->state.lr_flag != 2)
  2790. sd->magic_addele_script[type2] += val;
  2791. break;
  2792. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2793. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2794. if(sd->state.lr_flag != 2)
  2795. sd->magic_addrace[type2]+=val;
  2796. break;
  2797. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2798. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2799. if(sd->state.lr_flag != 2)
  2800. sd->magic_addclass[type2]+=val;
  2801. break;
  2802. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2803. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2804. if(sd->state.lr_flag != 2)
  2805. sd->magic_addsize[type2]+=val;
  2806. break;
  2807. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2808. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2809. if(sd->state.lr_flag != 2)
  2810. sd->magic_atk_ele[type2]+=val;
  2811. break;
  2812. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2813. switch (sd->state.lr_flag) {
  2814. case 0: //Right hand
  2815. 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);
  2816. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2817. {
  2818. 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));
  2819. break;
  2820. }
  2821. sd->right_weapon.add_dmg[i].class_ = type2;
  2822. sd->right_weapon.add_dmg[i].rate += val;
  2823. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2824. 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]));
  2825. break;
  2826. case 1: //Left hand
  2827. 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);
  2828. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2829. {
  2830. 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));
  2831. break;
  2832. }
  2833. sd->left_weapon.add_dmg[i].class_ = type2;
  2834. sd->left_weapon.add_dmg[i].rate += val;
  2835. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2836. 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]));
  2837. break;
  2838. }
  2839. break;
  2840. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2841. if(sd->state.lr_flag == 2)
  2842. break;
  2843. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2844. if (i == ARRAYLENGTH(sd->add_mdmg))
  2845. {
  2846. 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));
  2847. break;
  2848. }
  2849. sd->add_mdmg[i].class_ = type2;
  2850. sd->add_mdmg[i].rate += val;
  2851. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2852. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2853. break;
  2854. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2855. if(sd->state.lr_flag == 2)
  2856. break;
  2857. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2858. if (i == ARRAYLENGTH(sd->add_def))
  2859. {
  2860. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2861. break;
  2862. }
  2863. sd->add_def[i].class_ = type2;
  2864. sd->add_def[i].rate += val;
  2865. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2866. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2867. break;
  2868. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2869. if(sd->state.lr_flag == 2)
  2870. break;
  2871. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2872. if (i == ARRAYLENGTH(sd->add_mdef))
  2873. {
  2874. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2875. break;
  2876. }
  2877. sd->add_mdef[i].class_ = type2;
  2878. sd->add_mdef[i].rate += val;
  2879. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2880. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2881. break;
  2882. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2883. if(!sd->state.lr_flag) {
  2884. sd->right_weapon.hp_drain_rate.rate += type2;
  2885. sd->right_weapon.hp_drain_rate.per += val;
  2886. }
  2887. else if(sd->state.lr_flag == 1) {
  2888. sd->left_weapon.hp_drain_rate.rate += type2;
  2889. sd->left_weapon.hp_drain_rate.per += val;
  2890. }
  2891. break;
  2892. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2893. if(!sd->state.lr_flag) {
  2894. sd->right_weapon.sp_drain_rate.rate += type2;
  2895. sd->right_weapon.sp_drain_rate.per += val;
  2896. }
  2897. else if(sd->state.lr_flag == 1) {
  2898. sd->left_weapon.sp_drain_rate.rate += type2;
  2899. sd->left_weapon.sp_drain_rate.per += val;
  2900. }
  2901. break;
  2902. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2903. if(sd->state.lr_flag != 2) {
  2904. sd->bonus.sp_vanish_rate += type2;
  2905. sd->bonus.sp_vanish_per += val;
  2906. }
  2907. break;
  2908. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2909. if(sd->state.lr_flag != 2) {
  2910. sd->bonus.hp_vanish_rate += type2;
  2911. sd->bonus.hp_vanish_per += val;
  2912. }
  2913. break;
  2914. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2915. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2916. sd->bonus.get_zeny_rate = val;
  2917. sd->bonus.get_zeny_num = type2;
  2918. }
  2919. break;
  2920. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  2921. if(sd->state.lr_flag != 2) {
  2922. sd->bonus.get_zeny_rate += val;
  2923. sd->bonus.get_zeny_num += type2;
  2924. }
  2925. break;
  2926. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  2927. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  2928. if(sd->state.lr_flag == 2)
  2929. break;
  2930. sd->weapon_coma_ele[type2] += val;
  2931. sd->special_state.bonus_coma = 1;
  2932. break;
  2933. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  2934. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  2935. if(sd->state.lr_flag == 2)
  2936. break;
  2937. sd->weapon_coma_race[type2] += val;
  2938. sd->special_state.bonus_coma = 1;
  2939. break;
  2940. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  2941. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  2942. if(sd->state.lr_flag == 2)
  2943. break;
  2944. sd->weapon_coma_class[type2] += val;
  2945. sd->special_state.bonus_coma = 1;
  2946. break;
  2947. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  2948. if(sd->state.lr_flag != 2)
  2949. sd->weapon_atk[type2]+=val;
  2950. break;
  2951. case SP_WEAPON_ATK_RATE: // bonus2 bWeaponAtkRate,w,n;
  2952. if(sd->state.lr_flag != 2)
  2953. sd->weapon_atk_rate[type2]+=val;
  2954. break;
  2955. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  2956. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  2957. if(sd->state.lr_flag != 2)
  2958. sd->critaddrace[type2] += val*10;
  2959. break;
  2960. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  2961. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  2962. if(sd->state.lr_flag != 2)
  2963. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  2964. break;
  2965. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  2966. if(sd->state.lr_flag == 2)
  2967. break;
  2968. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2969. if (i == ARRAYLENGTH(sd->skillatk))
  2970. { //Better mention this so the array length can be updated. [Skotlex]
  2971. 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);
  2972. break;
  2973. }
  2974. if (sd->skillatk[i].id == type2)
  2975. sd->skillatk[i].val += val;
  2976. else {
  2977. sd->skillatk[i].id = type2;
  2978. sd->skillatk[i].val = val;
  2979. }
  2980. break;
  2981. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  2982. if(sd->state.lr_flag == 2)
  2983. break;
  2984. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2985. if (i == ARRAYLENGTH(sd->skillheal))
  2986. { // Better mention this so the array length can be updated. [Skotlex]
  2987. 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);
  2988. break;
  2989. }
  2990. if (sd->skillheal[i].id == type2)
  2991. sd->skillheal[i].val += val;
  2992. else {
  2993. sd->skillheal[i].id = type2;
  2994. sd->skillheal[i].val = val;
  2995. }
  2996. break;
  2997. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  2998. if(sd->state.lr_flag == 2)
  2999. break;
  3000. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3001. if (i == ARRAYLENGTH(sd->skillheal2))
  3002. { // Better mention this so the array length can be updated. [Skotlex]
  3003. 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);
  3004. break;
  3005. }
  3006. if (sd->skillheal2[i].id == type2)
  3007. sd->skillheal2[i].val += val;
  3008. else {
  3009. sd->skillheal2[i].id = type2;
  3010. sd->skillheal2[i].val = val;
  3011. }
  3012. break;
  3013. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3014. if(sd->state.lr_flag == 2)
  3015. break;
  3016. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3017. if (i == ARRAYLENGTH(sd->skillblown))
  3018. { //Better mention this so the array length can be updated. [Skotlex]
  3019. 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);
  3020. break;
  3021. }
  3022. if(sd->skillblown[i].id == type2)
  3023. sd->skillblown[i].val += val;
  3024. else {
  3025. sd->skillblown[i].id = type2;
  3026. sd->skillblown[i].val = val;
  3027. }
  3028. break;
  3029. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3030. if(sd->state.lr_flag != 2) {
  3031. sd->hp_loss.value = type2;
  3032. sd->hp_loss.rate = val;
  3033. }
  3034. break;
  3035. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3036. if(sd->state.lr_flag != 2) {
  3037. sd->hp_regen.value = type2;
  3038. sd->hp_regen.rate = val;
  3039. }
  3040. break;
  3041. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3042. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3043. if(sd->state.lr_flag != 2)
  3044. sd->right_weapon.addrace2[type2] += val;
  3045. else
  3046. sd->left_weapon.addrace2[type2] += val;
  3047. break;
  3048. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3049. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3050. if(sd->state.lr_flag != 2)
  3051. sd->subsize[type2]+=val;
  3052. break;
  3053. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3054. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3055. if(sd->state.lr_flag != 2)
  3056. sd->subrace2[type2]+=val;
  3057. break;
  3058. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3059. if(sd->state.lr_flag == 2)
  3060. break;
  3061. if (!itemdb_exists(type2)) {
  3062. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3063. break;
  3064. }
  3065. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3066. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3067. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3068. break;
  3069. }
  3070. sd->itemhealrate[i].nameid = type2;
  3071. sd->itemhealrate[i].rate += val;
  3072. break;
  3073. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3074. if (sd->state.lr_flag == 2)
  3075. break;
  3076. if (!type2 || !itemdb_group_exists(type2)) {
  3077. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3078. break;
  3079. }
  3080. pc_itemgrouphealrate(sd, type2, val);
  3081. break;
  3082. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3083. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3084. if(sd->state.lr_flag != 2)
  3085. sd->expaddrace[type2]+=val;
  3086. break;
  3087. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3088. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3089. if(sd->state.lr_flag != 2)
  3090. sd->expaddclass[type2]+=val;
  3091. break;
  3092. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3093. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3094. if(sd->state.lr_flag != 2)
  3095. sd->sp_gain_race[type2]+=val;
  3096. break;
  3097. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3098. if (sd->state.lr_flag != 2)
  3099. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3100. break;
  3101. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3102. if (sd->state.lr_flag != 2)
  3103. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3104. break;
  3105. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3106. if(sd->state.lr_flag != 2) {
  3107. sd->sp_loss.value = type2;
  3108. sd->sp_loss.rate = val;
  3109. }
  3110. break;
  3111. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3112. if(sd->state.lr_flag != 2) {
  3113. sd->sp_regen.value = type2;
  3114. sd->sp_regen.rate = val;
  3115. }
  3116. break;
  3117. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3118. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3119. if(!sd->state.lr_flag) {
  3120. sd->right_weapon.hp_drain_race[type2] += val;
  3121. }
  3122. else if(sd->state.lr_flag == 1) {
  3123. sd->left_weapon.hp_drain_race[type2] += val;
  3124. }
  3125. break;
  3126. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3127. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3128. if(!sd->state.lr_flag) {
  3129. sd->right_weapon.sp_drain_race[type2] += val;
  3130. }
  3131. else if(sd->state.lr_flag == 1) {
  3132. sd->left_weapon.sp_drain_race[type2] += val;
  3133. }
  3134. break;
  3135. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3136. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3137. if(!sd->state.lr_flag) {
  3138. sd->right_weapon.hp_drain_class[type2] += val;
  3139. }
  3140. else if(sd->state.lr_flag == 1) {
  3141. sd->left_weapon.hp_drain_class[type2] += val;
  3142. }
  3143. break;
  3144. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3145. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3146. if(!sd->state.lr_flag) {
  3147. sd->right_weapon.sp_drain_class[type2] += val;
  3148. }
  3149. else if(sd->state.lr_flag == 1) {
  3150. sd->left_weapon.sp_drain_class[type2] += val;
  3151. }
  3152. break;
  3153. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3154. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3155. if(sd->state.lr_flag != 2)
  3156. sd->ignore_mdef_by_race[type2] += val;
  3157. break;
  3158. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3159. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3160. if(sd->state.lr_flag != 2)
  3161. sd->ignore_mdef_by_class[type2] += val;
  3162. break;
  3163. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3164. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3165. if(sd->state.lr_flag != 2)
  3166. sd->ignore_def_by_race[type2] += val;
  3167. break;
  3168. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3169. if(sd->state.lr_flag == 2)
  3170. break;
  3171. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3172. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3173. 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);
  3174. break;
  3175. }
  3176. if (sd->skillusesprate[i].id == type2)
  3177. sd->skillusesprate[i].val += val;
  3178. else {
  3179. sd->skillusesprate[i].id = type2;
  3180. sd->skillusesprate[i].val = val;
  3181. }
  3182. break;
  3183. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3184. if(sd->state.lr_flag == 2)
  3185. break;
  3186. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3187. if (i == ARRAYLENGTH(sd->skillcooldown))
  3188. {
  3189. 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);
  3190. break;
  3191. }
  3192. if (sd->skillcooldown[i].id == type2)
  3193. sd->skillcooldown[i].val += val;
  3194. else {
  3195. sd->skillcooldown[i].id = type2;
  3196. sd->skillcooldown[i].val = val;
  3197. }
  3198. break;
  3199. #ifdef RENEWAL_CAST
  3200. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3201. if(sd->state.lr_flag == 2)
  3202. break;
  3203. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3204. if (i == ARRAYLENGTH(sd->skillfixcast))
  3205. {
  3206. 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);
  3207. break;
  3208. }
  3209. if (sd->skillfixcast[i].id == type2)
  3210. sd->skillfixcast[i].val += val;
  3211. else {
  3212. sd->skillfixcast[i].id = type2;
  3213. sd->skillfixcast[i].val = val;
  3214. }
  3215. break;
  3216. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3217. if(sd->state.lr_flag == 2)
  3218. break;
  3219. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3220. if (i == ARRAYLENGTH(sd->skillvarcast))
  3221. {
  3222. 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);
  3223. break;
  3224. }
  3225. if (sd->skillvarcast[i].id == type2)
  3226. sd->skillvarcast[i].val += val;
  3227. else {
  3228. sd->skillvarcast[i].id = type2;
  3229. sd->skillvarcast[i].val = val;
  3230. }
  3231. break;
  3232. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3233. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3234. if(sd->state.lr_flag == 2)
  3235. break;
  3236. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3237. if (i == ARRAYLENGTH(sd->skillcastrate))
  3238. {
  3239. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3240. break;
  3241. }
  3242. if(sd->skillcastrate[i].id == type2)
  3243. sd->skillcastrate[i].val -= val;
  3244. else {
  3245. sd->skillcastrate[i].id = type2;
  3246. sd->skillcastrate[i].val -= val;
  3247. }
  3248. break;
  3249. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3250. if(sd->state.lr_flag == 2)
  3251. break;
  3252. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3253. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3254. {
  3255. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3256. break;
  3257. }
  3258. if(sd->skillfixcastrate[i].id == type2)
  3259. sd->skillfixcastrate[i].val -= val;
  3260. else {
  3261. sd->skillfixcastrate[i].id = type2;
  3262. sd->skillfixcastrate[i].val -= val;
  3263. }
  3264. break;
  3265. #else
  3266. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3267. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3268. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3269. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3270. break;
  3271. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3272. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3273. if(sd->state.lr_flag == 2)
  3274. break;
  3275. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3276. if (i == ARRAYLENGTH(sd->skillcastrate))
  3277. { //Better mention this so the array length can be updated. [Skotlex]
  3278. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3279. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3280. break;
  3281. }
  3282. if(sd->skillcastrate[i].id == type2)
  3283. sd->skillcastrate[i].val += val;
  3284. else {
  3285. sd->skillcastrate[i].id = type2;
  3286. sd->skillcastrate[i].val = val;
  3287. }
  3288. break;
  3289. #endif
  3290. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3291. if(sd->state.lr_flag == 2)
  3292. break;
  3293. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3294. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3295. 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);
  3296. break;
  3297. }
  3298. if (sd->skillusesp[i].id == type2)
  3299. sd->skillusesp[i].val += val;
  3300. else {
  3301. sd->skillusesp[i].id = type2;
  3302. sd->skillusesp[i].val = val;
  3303. }
  3304. break;
  3305. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3306. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3307. if (i == ARRAYLENGTH(sd->subskill)) {
  3308. 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);
  3309. break;
  3310. }
  3311. if (sd->subskill[i].id == type2)
  3312. sd->subskill[i].val += val;
  3313. else {
  3314. sd->subskill[i].id = type2;
  3315. sd->subskill[i].val = val;
  3316. }
  3317. break;
  3318. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3319. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3320. sd->subdefele[type2] += val;
  3321. break;
  3322. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3323. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3324. sd->coma_class[type2] += val;
  3325. sd->special_state.bonus_coma = 1;
  3326. break;
  3327. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3328. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3329. sd->coma_race[type2] += val;
  3330. sd->special_state.bonus_coma = 1;
  3331. break;
  3332. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3333. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3334. if(sd->state.lr_flag != 2)
  3335. sd->magic_addrace2[type2] += val;
  3336. break;
  3337. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3338. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3339. if (sd->state.lr_flag != 2)
  3340. sd->ignore_mdef_by_race2[type2] += val;
  3341. break;
  3342. default:
  3343. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  3344. break;
  3345. }
  3346. }
  3347. /**
  3348. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3349. * @param sd
  3350. * @param type Bonus type used by bBonusName
  3351. * @param type2
  3352. * @param val Value that usually for rate or fixed value
  3353. */
  3354. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3355. {
  3356. nullpo_retv(sd);
  3357. switch(type){
  3358. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3359. if(sd->state.lr_flag != 2)
  3360. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3361. break;
  3362. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3363. if(sd->state.lr_flag != 2)
  3364. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3365. break;
  3366. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3367. if(sd->state.lr_flag != 2)
  3368. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3369. break;
  3370. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3371. if(sd->state.lr_flag != 2)
  3372. {
  3373. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3374. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3375. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3376. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3377. }
  3378. break;
  3379. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3380. if(sd->state.lr_flag != 2)
  3381. {
  3382. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3383. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3384. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3385. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3386. }
  3387. break;
  3388. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3389. if (sd->state.lr_flag != 2)
  3390. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3391. break;
  3392. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3393. if (sd->state.lr_flag != 2)
  3394. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3395. break;
  3396. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3397. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3398. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3399. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3400. break;
  3401. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3402. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3403. if(sd->state.lr_flag != 2)
  3404. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3405. break;
  3406. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3407. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3408. if( sd->state.lr_flag != 2 )
  3409. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3410. break;
  3411. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3412. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3413. if (sd->state.lr_flag != 2)
  3414. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3415. break;
  3416. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3417. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3418. if (sd->state.lr_flag != 2)
  3419. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3420. break;
  3421. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3422. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3423. if(sd->state.lr_flag != 2) {
  3424. sd->sp_vanish_race[type2].rate += type3;
  3425. sd->sp_vanish_race[type2].per += val;
  3426. }
  3427. break;
  3428. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3429. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3430. if(sd->state.lr_flag != 2) {
  3431. sd->hp_vanish_race[type2].rate += type3;
  3432. sd->hp_vanish_race[type2].per += val;
  3433. }
  3434. break;
  3435. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3436. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3437. if (sd->state.lr_flag == 2)
  3438. break;
  3439. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3440. //if (type3 > sd->norecover_state_race[type2].rate) {
  3441. // sd->norecover_state_race[type2].rate = type3;
  3442. // sd->norecover_state_race[type2].tick = val;
  3443. // break;
  3444. //}
  3445. sd->norecover_state_race[type2].rate = type3;
  3446. sd->norecover_state_race[type2].tick = val;
  3447. break;
  3448. default:
  3449. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3450. break;
  3451. }
  3452. }
  3453. /**
  3454. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3455. * @param sd
  3456. * @param type Bonus type used by bBonusName
  3457. * @param type2
  3458. * @param type3
  3459. * @param val Value that usually for rate or fixed value
  3460. */
  3461. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3462. {
  3463. nullpo_retv(sd);
  3464. switch(type){
  3465. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3466. if(sd->state.lr_flag != 2)
  3467. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3468. break;
  3469. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3470. if(sd->state.lr_flag != 2)
  3471. 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);
  3472. break;
  3473. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3474. if(sd->state.lr_flag != 2)
  3475. {
  3476. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3477. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3478. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3479. }
  3480. break;
  3481. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3482. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3483. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3484. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3485. break;
  3486. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3487. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3488. if (sd->state.lr_flag != 2)
  3489. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3490. break;
  3491. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3492. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3493. if( sd->state.lr_flag != 2 )
  3494. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3495. break;
  3496. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3497. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3498. if(sd->state.lr_flag == 2)
  3499. break;
  3500. sd->def_set_race[type2].rate = type3;
  3501. sd->def_set_race[type2].tick = type4;
  3502. sd->def_set_race[type2].value = val;
  3503. break;
  3504. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3505. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3506. if(sd->state.lr_flag == 2)
  3507. break;
  3508. sd->mdef_set_race[type2].rate = type3;
  3509. sd->mdef_set_race[type2].tick = type4;
  3510. sd->mdef_set_race[type2].value = val;
  3511. break;
  3512. default:
  3513. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3514. break;
  3515. }
  3516. }
  3517. /**
  3518. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3519. * @param sd
  3520. * @param type Bonus type used by bBonusName
  3521. * @param type2
  3522. * @param type3
  3523. * @param type4
  3524. * @param val Value that usually for rate or fixed value
  3525. */
  3526. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3527. {
  3528. nullpo_retv(sd);
  3529. switch(type){
  3530. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3531. if(sd->state.lr_flag != 2)
  3532. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3533. break;
  3534. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3535. if(sd->state.lr_flag != 2)
  3536. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3537. break;
  3538. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3539. if(sd->state.lr_flag != 2)
  3540. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3541. break;
  3542. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3543. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3544. if( sd->state.lr_flag != 2 )
  3545. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3546. break;
  3547. default:
  3548. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3549. break;
  3550. }
  3551. }
  3552. /*==========================================
  3553. * Grants a player a given skill. Flag values are:
  3554. * 0 - Grant permanent skill to be bound to skill tree
  3555. * 1 - Grant an item skill (temporary)
  3556. * 2 - Like 1, except the level granted can stack with previously learned level.
  3557. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3558. *------------------------------------------*/
  3559. bool pc_skill(TBL_PC* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3560. uint16 idx = 0;
  3561. nullpo_ret(sd);
  3562. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3563. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3564. return false;
  3565. }
  3566. if (level > MAX_SKILL_LEVEL) {
  3567. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3568. return false;
  3569. }
  3570. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3571. 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);
  3572. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3573. }
  3574. switch (type) {
  3575. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3576. sd->status.skill[idx].id = skill_id;
  3577. sd->status.skill[idx].lv = level;
  3578. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3579. if (level == 0) { //Remove skill.
  3580. sd->status.skill[idx].id = 0;
  3581. clif_deleteskill(sd,skill_id);
  3582. } else
  3583. clif_addskill(sd,skill_id);
  3584. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3585. status_calc_pc(sd, SCO_NONE);
  3586. break;
  3587. case ADDSKILL_TEMP: //Item bonus skill.
  3588. if (sd->status.skill[idx].id != 0) {
  3589. if (sd->status.skill[idx].lv >= level)
  3590. return true;
  3591. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3592. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3593. } else {
  3594. sd->status.skill[idx].id = skill_id;
  3595. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3596. }
  3597. sd->status.skill[idx].lv = level;
  3598. break;
  3599. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3600. if (sd->status.skill[idx].id != 0) {
  3601. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3602. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3603. } else {
  3604. sd->status.skill[idx].id = skill_id;
  3605. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3606. }
  3607. sd->status.skill[idx].lv += level;
  3608. break;
  3609. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3610. sd->status.skill[idx].id = skill_id;
  3611. sd->status.skill[idx].lv = level;
  3612. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3613. if (level == 0) { //Remove skill.
  3614. sd->status.skill[idx].id = 0;
  3615. clif_deleteskill(sd,skill_id);
  3616. } else
  3617. clif_addskill(sd,skill_id);
  3618. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3619. status_calc_pc(sd, SCO_NONE);
  3620. break;
  3621. default:
  3622. return false;
  3623. }
  3624. return true;
  3625. }
  3626. /*==========================================
  3627. * Append a card to an item ?
  3628. *------------------------------------------*/
  3629. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3630. {
  3631. int i;
  3632. unsigned short nameid;
  3633. nullpo_ret(sd);
  3634. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3635. return 0; //Invalid item index.
  3636. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3637. return 0; //Invalid card index.
  3638. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3639. return 0; // target item missing
  3640. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3641. return 0; // target card missing
  3642. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3643. return 0; // only weapons and armor are allowed
  3644. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3645. return 0; // must be a card
  3646. if( sd->status.inventory[idx_equip].identify == 0 )
  3647. return 0; // target must be identified
  3648. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3649. return 0; // card slots reserved for other purposes
  3650. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3651. return 0; // card cannot be compounded on this item type
  3652. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3653. return 0; // attempted to place shield card on left-hand weapon.
  3654. if( sd->status.inventory[idx_equip].equip != 0 )
  3655. return 0; // item must be unequipped
  3656. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3657. if( i == sd->inventory_data[idx_equip]->slot )
  3658. return 0; // no free slots
  3659. // remember the card id to insert
  3660. nameid = sd->status.inventory[idx_card].nameid;
  3661. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3662. {// failed
  3663. clif_insert_card(sd,idx_equip,idx_card,1);
  3664. }
  3665. else
  3666. {// success
  3667. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3668. sd->status.inventory[idx_equip].card[i] = nameid;
  3669. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3670. clif_insert_card(sd,idx_equip,idx_card,0);
  3671. }
  3672. return 0;
  3673. }
  3674. //
  3675. // Items
  3676. //
  3677. /*==========================================
  3678. * Update buying value by skills
  3679. *------------------------------------------*/
  3680. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3681. {
  3682. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3683. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3684. rate1 = 5+skill*2-((skill==10)? 1:0);
  3685. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3686. rate2 = 5+skill*4;
  3687. if(rate1 < rate2) rate1 = rate2;
  3688. if(rate1)
  3689. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3690. if(val < 0) val = 0;
  3691. if(orig_value > 0 && val < 1) val = 1;
  3692. return val;
  3693. }
  3694. /*==========================================
  3695. * Update selling value by skills
  3696. *------------------------------------------*/
  3697. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3698. {
  3699. int skill,val = orig_value,rate = 0;
  3700. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3701. rate = 5+skill*2-((skill==10)? 1:0);
  3702. if(rate)
  3703. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3704. if(val < 0) val = 0;
  3705. if(orig_value > 0 && val < 1) val = 1;
  3706. return val;
  3707. }
  3708. /*==========================================
  3709. * Checking if we have enough place on inventory for new item
  3710. * Make sure to take 30k as limit (for client I guess)
  3711. * @param sd
  3712. * @param nameid
  3713. * @param amount
  3714. * @return e_chkitem_result
  3715. *------------------------------------------*/
  3716. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3717. {
  3718. int i;
  3719. struct item_data* data;
  3720. nullpo_ret(sd);
  3721. if(amount > MAX_AMOUNT)
  3722. return CHKADDITEM_OVERAMOUNT;
  3723. data = itemdb_search(nameid);
  3724. if(!itemdb_isstackable2(data))
  3725. return CHKADDITEM_NEW;
  3726. if( data->stack.inventory && amount > data->stack.amount )
  3727. return CHKADDITEM_OVERAMOUNT;
  3728. for(i=0;i<MAX_INVENTORY;i++){
  3729. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3730. if(sd->status.inventory[i].nameid==nameid){
  3731. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3732. return CHKADDITEM_OVERAMOUNT;
  3733. return CHKADDITEM_EXIST;
  3734. }
  3735. }
  3736. return CHKADDITEM_NEW;
  3737. }
  3738. /*==========================================
  3739. * Return number of available place in inventory
  3740. * Each non stackable item will reduce place by 1
  3741. * @param sd
  3742. * @return Number of empty slots
  3743. *------------------------------------------*/
  3744. uint8 pc_inventoryblank(struct map_session_data *sd)
  3745. {
  3746. uint8 i, b;
  3747. nullpo_ret(sd);
  3748. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3749. if(sd->status.inventory[i].nameid==0)
  3750. b++;
  3751. }
  3752. return b;
  3753. }
  3754. /**
  3755. * Attempts to remove zeny from player
  3756. * @param sd: Player
  3757. * @param zeny: Zeny removed
  3758. * @param type: Log type
  3759. * @param tsd: (optional) From who to log (if null take sd)
  3760. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3761. */
  3762. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3763. {
  3764. nullpo_retr(2,sd);
  3765. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3766. if( zeny < 0 )
  3767. {
  3768. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3769. return 1;
  3770. }
  3771. if( sd->status.zeny < zeny )
  3772. return 1; //Not enough.
  3773. sd->status.zeny -= zeny;
  3774. clif_updatestatus(sd,SP_ZENY);
  3775. if(!tsd) tsd = sd;
  3776. log_zeny(sd, type, tsd, -zeny);
  3777. if( zeny > 0 && sd->state.showzeny ) {
  3778. char output[255];
  3779. sprintf(output, "Removed %dz.", zeny);
  3780. clif_disp_onlyself(sd,output,strlen(output));
  3781. }
  3782. return 0;
  3783. }
  3784. /**
  3785. * Attempts to give zeny to player
  3786. * @param sd: Player
  3787. * @param type: Log type
  3788. * @param tsd: (optional) From who to log (if null take sd)
  3789. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3790. */
  3791. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3792. {
  3793. nullpo_retr(-1,sd);
  3794. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3795. if( zeny < 0 )
  3796. {
  3797. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3798. return 1;
  3799. }
  3800. if( zeny > MAX_ZENY - sd->status.zeny )
  3801. zeny = MAX_ZENY - sd->status.zeny;
  3802. sd->status.zeny += zeny;
  3803. clif_updatestatus(sd,SP_ZENY);
  3804. if(!tsd) tsd = sd;
  3805. log_zeny(sd, type, tsd, zeny);
  3806. if( zeny > 0 && sd->state.showzeny ) {
  3807. char output[255];
  3808. sprintf(output, "Gained %dz.", zeny);
  3809. clif_disp_onlyself(sd,output,strlen(output));
  3810. }
  3811. return 0;
  3812. }
  3813. /**
  3814. * Attempts to remove Cash Points from player
  3815. * @param sd: Player
  3816. * @param price: Points player has to pay
  3817. * @param points: Points player has
  3818. * @param type: Log type
  3819. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3820. */
  3821. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3822. {
  3823. int cash;
  3824. nullpo_retr(-1,sd);
  3825. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3826. if( price < 0 || points < 0 )
  3827. {
  3828. 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);
  3829. return -2;
  3830. }
  3831. if( points > price )
  3832. {
  3833. 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);
  3834. points = price;
  3835. }
  3836. cash = price-points;
  3837. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3838. {
  3839. 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);
  3840. return -1;
  3841. }
  3842. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints-cash);
  3843. if( cash ){
  3844. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3845. }
  3846. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints-points);
  3847. if( points ){
  3848. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3849. }
  3850. if( battle_config.cashshop_show_points )
  3851. {
  3852. char output[CHAT_SIZE_MAX];
  3853. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3854. clif_disp_onlyself(sd, output, strlen(output));
  3855. }
  3856. return cash+points;
  3857. }
  3858. /**
  3859. * Attempts to give Cash Points to player
  3860. * @param sd: Player
  3861. * @param cash: Cash player gets
  3862. * @param points: Points player has
  3863. * @param type: Log type
  3864. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  3865. */
  3866. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  3867. {
  3868. char output[CHAT_SIZE_MAX];
  3869. nullpo_retr(-1,sd);
  3870. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3871. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3872. if( cash > 0 )
  3873. {
  3874. if( cash > MAX_ZENY-sd->cashPoints )
  3875. {
  3876. 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);
  3877. cash = MAX_ZENY-sd->cashPoints;
  3878. }
  3879. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints+cash);
  3880. if( cash ){
  3881. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3882. }
  3883. if( battle_config.cashshop_show_points )
  3884. {
  3885. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3886. clif_disp_onlyself(sd, output, strlen(output));
  3887. }
  3888. return cash;
  3889. }
  3890. else if( cash < 0 )
  3891. {
  3892. 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);
  3893. return -1;
  3894. }
  3895. if( points > 0 )
  3896. {
  3897. if( points > MAX_ZENY-sd->kafraPoints )
  3898. {
  3899. 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);
  3900. points = MAX_ZENY-sd->kafraPoints;
  3901. }
  3902. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints+points);
  3903. if( points ){
  3904. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3905. }
  3906. if( battle_config.cashshop_show_points )
  3907. {
  3908. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3909. clif_disp_onlyself(sd, output, strlen(output));
  3910. }
  3911. return points;
  3912. }
  3913. else if( points < 0 )
  3914. {
  3915. 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);
  3916. return -1;
  3917. }
  3918. return -2; //shouldn't happen but just in case
  3919. }
  3920. /**
  3921. * Searching a specified itemid in inventory and return his stored index
  3922. * @param sd Player
  3923. * @param nameid Find this Item!
  3924. * @return Stored index in inventory, or -1 if not found.
  3925. **/
  3926. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  3927. short i;
  3928. nullpo_retr(-1, sd);
  3929. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && (sd->status.inventory[i].amount > 0 || nameid == 0) );
  3930. return ( i < MAX_INVENTORY ) ? i : -1;
  3931. }
  3932. /** Attempt to add a new item to player inventory
  3933. * @param sd
  3934. * @param item_data
  3935. * @param amount
  3936. * @param log_type
  3937. * @return
  3938. * 0 = success
  3939. * 1 = invalid itemid not found or negative amount
  3940. * 2 = overweight
  3941. * 3 = ?
  3942. * 4 = no free place found
  3943. * 5 = max amount reached
  3944. * 6 = ?
  3945. * 7 = stack limitation
  3946. */
  3947. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  3948. struct item_data *id;
  3949. int16 i;
  3950. unsigned int w;
  3951. nullpo_retr(1, sd);
  3952. nullpo_retr(1, item);
  3953. if( item->nameid == 0 || amount <= 0 )
  3954. return ADDITEM_INVALID;
  3955. if( amount > MAX_AMOUNT )
  3956. return ADDITEM_OVERAMOUNT;
  3957. id = itemdb_search(item->nameid);
  3958. if( id->stack.inventory && amount > id->stack.amount )
  3959. {// item stack limitation
  3960. return ADDITEM_STACKLIMIT;
  3961. }
  3962. w = id->weight*amount;
  3963. if(sd->weight + w > sd->max_weight)
  3964. return ADDITEM_OVERWEIGHT;
  3965. i = MAX_INVENTORY;
  3966. if (id->flag.guid && !item->unique_id)
  3967. item->unique_id = pc_generate_unique_id(sd);
  3968. // Stackable | Non Rental
  3969. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  3970. for( i = 0; i < MAX_INVENTORY; i++ ) {
  3971. if( sd->status.inventory[i].nameid == item->nameid &&
  3972. sd->status.inventory[i].bound == item->bound &&
  3973. sd->status.inventory[i].expire_time == 0 &&
  3974. sd->status.inventory[i].unique_id == item->unique_id &&
  3975. memcmp(&sd->status.inventory[i].card, &item->card, sizeof(item->card)) == 0
  3976. )
  3977. {
  3978. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->status.inventory[i].amount ) )
  3979. return ADDITEM_OVERAMOUNT;
  3980. sd->status.inventory[i].amount += amount;
  3981. clif_additem(sd,i,amount,0);
  3982. break;
  3983. }
  3984. }
  3985. }
  3986. if (i >= MAX_INVENTORY) {
  3987. i = pc_search_inventory(sd,0);
  3988. if( i < 0 )
  3989. return ADDITEM_OVERITEM;
  3990. memcpy(&sd->status.inventory[i], item, sizeof(sd->status.inventory[0]));
  3991. // clear equip and favorite fields first, just in case
  3992. if( item->equip )
  3993. sd->status.inventory[i].equip = 0;
  3994. if( item->favorite )
  3995. sd->status.inventory[i].favorite = 0;
  3996. sd->status.inventory[i].amount = amount;
  3997. sd->inventory_data[i] = id;
  3998. sd->last_addeditem_index = i;
  3999. if (!itemdb_isstackable2(id) || id->flag.guid)
  4000. sd->status.inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4001. clif_additem(sd,i,amount,0);
  4002. }
  4003. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  4004. sd->weight += w;
  4005. clif_updatestatus(sd,SP_WEIGHT);
  4006. //Auto-equip
  4007. if(id->flag.autoequip)
  4008. pc_equipitem(sd, i, id->equip);
  4009. /* rental item check */
  4010. if( item->expire_time ) {
  4011. if( time(NULL) > item->expire_time ) {
  4012. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  4013. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4014. } else {
  4015. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4016. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  4017. pc_inventory_rental_add(sd, seconds);
  4018. }
  4019. }
  4020. return ADDITEM_SUCCESS;
  4021. }
  4022. /*==========================================
  4023. * Remove an item at index n from inventory by amount.
  4024. * @param sd
  4025. * @param n Item index in inventory
  4026. * @param amount
  4027. * @param type &1: Don't notify deletion; &2 Don't notify weight change
  4028. * @param reason Delete reason
  4029. * @param log_type e_log_pick_type
  4030. * @return 1 - invalid itemid or negative amount; 0 - Success
  4031. *------------------------------------------*/
  4032. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4033. {
  4034. nullpo_retr(1, sd);
  4035. if(n < 0 || sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4036. return 1;
  4037. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  4038. sd->status.inventory[n].amount -= amount;
  4039. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4040. if( sd->status.inventory[n].amount <= 0 ){
  4041. if(sd->status.inventory[n].equip)
  4042. pc_unequipitem(sd,n,3);
  4043. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  4044. sd->inventory_data[n] = NULL;
  4045. }
  4046. if(!(type&1))
  4047. clif_delitem(sd,n,amount,reason);
  4048. if(!(type&2))
  4049. clif_updatestatus(sd,SP_WEIGHT);
  4050. return 0;
  4051. }
  4052. /*==========================================
  4053. * Attempt to drop an item.
  4054. * @param sd
  4055. * @param n Item index in inventory
  4056. * @param amount Amount of item
  4057. * @return False = fail; True = success
  4058. *------------------------------------------*/
  4059. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4060. {
  4061. nullpo_retr(1, sd);
  4062. if(n < 0 || n >= MAX_INVENTORY)
  4063. return false;
  4064. if(amount <= 0)
  4065. return false;
  4066. if(sd->status.inventory[n].nameid <= 0 ||
  4067. sd->status.inventory[n].amount <= 0 ||
  4068. sd->status.inventory[n].amount < amount ||
  4069. sd->state.trading || sd->state.vending ||
  4070. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4071. )
  4072. return false;
  4073. if( map[sd->bl.m].flag.nodrop )
  4074. {
  4075. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4076. return false; //Can't drop items in nodrop mapflag maps.
  4077. }
  4078. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  4079. {
  4080. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4081. return false;
  4082. }
  4083. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4084. return false;
  4085. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4086. clif_dropitem(sd, n, amount);
  4087. return true;
  4088. }
  4089. /*==========================================
  4090. * Attempt to pick up an item.
  4091. * @param sd
  4092. * @param fitem Item that will be picked
  4093. * @return False = fail; True = success
  4094. *------------------------------------------*/
  4095. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4096. {
  4097. int flag = 0;
  4098. unsigned int tick = gettick();
  4099. struct party_data *p = NULL;
  4100. nullpo_ret(sd);
  4101. nullpo_ret(fitem);
  4102. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4103. return false; // Distance is too far
  4104. if (sd->sc.cant.pickup)
  4105. return false;
  4106. if (sd->status.party_id)
  4107. p = party_search(sd->status.party_id);
  4108. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4109. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4110. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4111. if (!(p && p->party.item&1 &&
  4112. first_sd && first_sd->status.party_id == sd->status.party_id
  4113. ))
  4114. return false;
  4115. }
  4116. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4117. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4118. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4119. if (!(p && p->party.item&1 &&
  4120. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4121. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4122. ))
  4123. return false;
  4124. }
  4125. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4126. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4127. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4128. if(!(p && p->party.item&1 &&
  4129. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4130. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4131. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4132. ))
  4133. return false;
  4134. }
  4135. }
  4136. }
  4137. }
  4138. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4139. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4140. clif_additem(sd,0,0,flag);
  4141. return true;
  4142. }
  4143. //Display pickup animation.
  4144. pc_stop_attack(sd);
  4145. clif_takeitem(&sd->bl,&fitem->bl);
  4146. if (fitem->mob_id &&
  4147. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4148. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4149. )
  4150. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4151. map_clearflooritem(&fitem->bl);
  4152. return true;
  4153. }
  4154. /*==========================================
  4155. * Check if item is usable.
  4156. * Return:
  4157. * 0 = no
  4158. * 1 = yes
  4159. *------------------------------------------*/
  4160. bool pc_isUseitem(struct map_session_data *sd,int n)
  4161. {
  4162. struct item_data *item;
  4163. unsigned short nameid;
  4164. nullpo_ret(sd);
  4165. item = sd->inventory_data[n];
  4166. nameid = sd->status.inventory[n].nameid;
  4167. if( item == NULL )
  4168. return false;
  4169. //Not consumable item
  4170. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4171. return false;
  4172. if( !item->script ) //if it has no script, you can't really consume it!
  4173. return false;
  4174. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4175. return true;
  4176. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4177. return false;
  4178. //Prevent mass item usage. [Skotlex]
  4179. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4180. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4181. )
  4182. return false;
  4183. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4184. clif_msg(sd,ITEM_NOUSE_SITTING);
  4185. return false; // You cannot use this item while sitting.
  4186. }
  4187. if (sd->state.storage_flag && item->type != IT_CASH) {
  4188. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd,388));
  4189. return false; // You cannot use this item while storage is open.
  4190. }
  4191. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m)))
  4192. return false;
  4193. switch( nameid ) {
  4194. case ITEMID_ANODYNE:
  4195. if( map_flag_gvg(sd->bl.m) )
  4196. return false;
  4197. case ITEMID_ALOEBERA:
  4198. if( pc_issit(sd) )
  4199. return false;
  4200. break;
  4201. case ITEMID_WING_OF_FLY:
  4202. case ITEMID_GIANT_FLY_WING:
  4203. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) ) {
  4204. clif_skill_teleportmessage(sd,0);
  4205. return false;
  4206. }
  4207. case ITEMID_WING_OF_BUTTERFLY:
  4208. case ITEMID_DUN_TELE_SCROLL1:
  4209. case ITEMID_DUN_TELE_SCROLL2:
  4210. case ITEMID_DUN_TELE_SCROLL3:
  4211. case ITEMID_WOB_RUNE:
  4212. case ITEMID_WOB_SCHWALTZ:
  4213. case ITEMID_WOB_RACHEL:
  4214. case ITEMID_WOB_LOCAL:
  4215. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4216. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4217. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4218. return false;
  4219. }
  4220. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  4221. return false;
  4222. break;
  4223. case ITEMID_BUBBLE_GUM:
  4224. case ITEMID_COMP_BUBBLE_GUM:
  4225. if( sd->sc.data[SC_ITEMBOOST] )
  4226. return false;
  4227. break;
  4228. case ITEMID_BATTLE_MANUAL:
  4229. case ITEMID_COMP_BATTLE_MANUAL:
  4230. case ITEMID_THICK_BATTLE_MANUAL:
  4231. case ITEMID_NOBLE_NAMEPLATE:
  4232. case ITEMID_BATTLE_MANUAL25:
  4233. case ITEMID_BATTLE_MANUAL100:
  4234. case ITEMID_BATTLE_MANUAL300:
  4235. if( sd->sc.data[SC_EXPBOOST] )
  4236. return false;
  4237. break;
  4238. case ITEMID_JOB_MANUAL50:
  4239. if( sd->sc.data[SC_JEXPBOOST] )
  4240. return false;
  4241. break;
  4242. case ITEMID_MERCENARY_RED_POTION:
  4243. case ITEMID_MERCENARY_BLUE_POTION:
  4244. case ITEMID_M_CENTER_POTION:
  4245. case ITEMID_M_AWAKENING_POTION:
  4246. case ITEMID_M_BERSERK_POTION:
  4247. if( sd->md == NULL || sd->md->db == NULL )
  4248. return false;
  4249. if( sd->md->sc.data[SC_BERSERK] )
  4250. return false;
  4251. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4252. return false;
  4253. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4254. return false;
  4255. break;
  4256. case ITEMID_NEURALIZER:
  4257. if( !map[sd->bl.m].flag.reset )
  4258. return false;
  4259. break;
  4260. }
  4261. if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL )
  4262. return false; // Mercenary Scrolls
  4263. /**
  4264. * Only Rune Knights may use runes
  4265. **/
  4266. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  4267. return false;
  4268. /**
  4269. * Only GCross may use poisons
  4270. **/
  4271. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  4272. return false;
  4273. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4274. if( pc_is90overweight(sd) ) {
  4275. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4276. return false;
  4277. }
  4278. if( !pc_inventoryblank(sd) ) {
  4279. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 732)); //Item cannot be open when inventory is full
  4280. return false;
  4281. }
  4282. }
  4283. //Gender check
  4284. if(item->sex != 2 && sd->status.sex != item->sex)
  4285. return false;
  4286. //Required level check
  4287. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4288. return false;
  4289. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4290. return false;
  4291. //Not equipable by class. [Skotlex]
  4292. if (!(
  4293. (1<<(sd->class_&MAPID_BASEMASK)) &
  4294. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4295. ))
  4296. return false;
  4297. if (sd->sc.count && (
  4298. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4299. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4300. sd->sc.data[SC_TRICKDEAD] ||
  4301. sd->sc.data[SC_HIDING] ||
  4302. sd->sc.data[SC__SHADOWFORM] ||
  4303. sd->sc.data[SC__INVISIBILITY] ||
  4304. sd->sc.data[SC__MANHOLE] ||
  4305. sd->sc.data[SC_KAGEHUMI] ||
  4306. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4307. sd->sc.data[SC_HEAT_BARREL_AFTER] ||
  4308. sd->sc.data[SC_KINGS_GRACE] ||
  4309. sd->sc.data[SC_SUHIDE]))
  4310. return false;
  4311. if (!pc_isItemClass(sd,item))
  4312. return false;
  4313. //Dead Branch items
  4314. if( item->flag.dead_branch )
  4315. log_branch(sd);
  4316. return true;
  4317. }
  4318. /*==========================================
  4319. * Last checks to use an item.
  4320. * Return:
  4321. * 0 = fail
  4322. * 1 = success
  4323. *------------------------------------------*/
  4324. int pc_useitem(struct map_session_data *sd,int n)
  4325. {
  4326. unsigned int tick = gettick();
  4327. int amount;
  4328. unsigned short nameid;
  4329. struct script_code *script;
  4330. struct item item;
  4331. struct item_data *id;
  4332. nullpo_ret(sd);
  4333. if (sd->npc_id) {
  4334. #ifdef RENEWAL
  4335. clif_msg(sd, USAGE_FAIL); // TODO look for the client date that has this message.
  4336. return 0;
  4337. #else
  4338. if( !sd->npc_item_flag )
  4339. return 0;
  4340. #endif
  4341. }
  4342. item = sd->status.inventory[n];
  4343. id = sd->inventory_data[n];
  4344. if (item.nameid == 0 || item.amount <= 0)
  4345. return 0;
  4346. if( !pc_isUseitem(sd,n) )
  4347. return 0;
  4348. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4349. nameid = id->nameid;
  4350. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4351. return 0;
  4352. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4353. if( id->flag.delay_consume ) {
  4354. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4355. return 0;
  4356. else if( pc_issit(sd) )
  4357. return 0;
  4358. }
  4359. //Since most delay-consume items involve using a "skill-type" target cursor,
  4360. //perform a skill-use check before going through. [Skotlex]
  4361. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4362. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4363. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4364. return 0;
  4365. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4366. return 0;
  4367. /* on restricted maps the item is consumed but the effect is not used */
  4368. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4369. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4370. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4371. clif_useitemack(sd,n,item.amount-1,true);
  4372. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4373. }
  4374. return 0;/* regardless, effect is not run */
  4375. }
  4376. sd->itemid = item.nameid;
  4377. sd->itemindex = n;
  4378. if(sd->catch_target_class != -1) //Abort pet catching.
  4379. sd->catch_target_class = -1;
  4380. amount = item.amount;
  4381. script = id->script;
  4382. //Check if the item is to be consumed immediately [Skotlex]
  4383. if (id->flag.delay_consume)
  4384. clif_useitemack(sd, n, amount, true);
  4385. else
  4386. {
  4387. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4388. {
  4389. clif_useitemack(sd, n, amount - 1, true);
  4390. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4391. }
  4392. else
  4393. clif_useitemack(sd, n, 0, false);
  4394. }
  4395. if(item.card[0]==CARD0_CREATE &&
  4396. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4397. {
  4398. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4399. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4400. potion_flag = 3; //Even more effective potions.
  4401. }
  4402. //Update item use time.
  4403. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4404. if( itemdb_iscashfood(nameid) )
  4405. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4406. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4407. potion_flag = 0;
  4408. return 1;
  4409. }
  4410. /**
  4411. * Add item on cart for given index.
  4412. * @param sd
  4413. * @param item
  4414. * @param amount
  4415. * @param log_type
  4416. * @return 0 = success; 1 = fail; 2 = no slot
  4417. */
  4418. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4419. {
  4420. struct item_data *data;
  4421. int i,w;
  4422. nullpo_retr(1, sd);
  4423. nullpo_retr(1, item);
  4424. if(item->nameid == 0 || amount <= 0)
  4425. return 1;
  4426. data = itemdb_search(item->nameid);
  4427. if( data->stack.cart && amount > data->stack.amount )
  4428. {// item stack limitation
  4429. return 1;
  4430. }
  4431. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4432. { // Check item trade restrictions [Skotlex]
  4433. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4434. return 1;
  4435. }
  4436. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4437. return 1;
  4438. // ID no longer points to inventory/kafra ID. While we get a new one we don't want to mess up vending creation.
  4439. item->id = 0;
  4440. i = MAX_CART;
  4441. if( itemdb_isstackable2(data) && !item->expire_time )
  4442. {
  4443. for (i = 0; i < MAX_CART; i++) {
  4444. if (sd->status.cart[i].nameid == item->nameid
  4445. && sd->status.cart[i].bound == item->bound
  4446. && sd->status.cart[i].unique_id == item->unique_id
  4447. && memcmp(sd->status.cart[i].card, item->card, sizeof(item->card)) == 0
  4448. )
  4449. break;
  4450. }
  4451. }
  4452. if( i < MAX_CART )
  4453. {// item already in cart, stack it
  4454. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4455. return 2; // no slot
  4456. sd->status.cart[i].amount+=amount;
  4457. clif_cart_additem(sd,i,amount,0);
  4458. }
  4459. else
  4460. {// item not stackable or not present, add it
  4461. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4462. if( i == MAX_CART )
  4463. return 2; // no slot
  4464. memcpy(&sd->status.cart[i],item,sizeof(sd->status.cart[0]));
  4465. sd->status.cart[i].amount=amount;
  4466. sd->cart_num++;
  4467. clif_cart_additem(sd,i,amount,0);
  4468. }
  4469. sd->status.cart[i].favorite = 0;/* clear */
  4470. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4471. sd->cart_weight += w;
  4472. clif_updatestatus(sd,SP_CARTINFO);
  4473. return 0;
  4474. }
  4475. /*==========================================
  4476. * Delete item on cart for given index.
  4477. *------------------------------------------*/
  4478. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4479. {
  4480. nullpo_retv(sd);
  4481. if(sd->status.cart[n].nameid == 0 ||
  4482. sd->status.cart[n].amount<amount)
  4483. return;
  4484. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4485. sd->status.cart[n].amount -= amount;
  4486. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4487. if(sd->status.cart[n].amount <= 0){
  4488. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4489. sd->cart_num--;
  4490. }
  4491. if(!type) {
  4492. clif_cart_delitem(sd,n,amount);
  4493. clif_updatestatus(sd,SP_CARTINFO);
  4494. }
  4495. }
  4496. /*==========================================
  4497. * Transfer item from inventory to cart.
  4498. *------------------------------------------*/
  4499. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4500. {
  4501. struct item *item_data;
  4502. char flag;
  4503. nullpo_retv(sd);
  4504. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4505. return;
  4506. item_data = &sd->status.inventory[idx];
  4507. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4508. return;
  4509. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4510. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4511. else {
  4512. clif_dropitem(sd,idx,0);
  4513. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4514. }
  4515. }
  4516. /*==========================================
  4517. * Get number of item in cart.
  4518. * Return:
  4519. -1 = itemid not found or no amount found
  4520. x = remaining itemid on cart after get
  4521. *------------------------------------------*/
  4522. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4523. {
  4524. struct item* item_data;
  4525. nullpo_retr(-1, sd);
  4526. item_data = &sd->status.cart[idx];
  4527. if( item_data->nameid == 0 || item_data->amount == 0 )
  4528. return -1;
  4529. return item_data->amount - amount;
  4530. }
  4531. /*==========================================
  4532. * Retrieve an item at index idx from cart.
  4533. *------------------------------------------*/
  4534. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4535. {
  4536. struct item *item_data;
  4537. unsigned char flag = 0;
  4538. nullpo_retv(sd);
  4539. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4540. return;
  4541. item_data=&sd->status.cart[idx];
  4542. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4543. return;
  4544. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4545. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4546. else {
  4547. clif_dropitem(sd,idx,0);
  4548. clif_additem(sd,0,0,flag);
  4549. }
  4550. }
  4551. /*==========================================
  4552. * Bound Item Check
  4553. * Type:
  4554. * 1 Account Bound
  4555. * 2 Guild Bound
  4556. * 3 Party Bound
  4557. * 4 Character Bound
  4558. *------------------------------------------*/
  4559. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4560. {
  4561. int i=0, j=0;
  4562. for(i=0;i<MAX_INVENTORY;i++){
  4563. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4564. idxlist[j] = i;
  4565. j++;
  4566. }
  4567. }
  4568. return j;
  4569. }
  4570. /*==========================================
  4571. * Display item stolen msg to player sd
  4572. *------------------------------------------*/
  4573. int pc_show_steal(struct block_list *bl,va_list ap)
  4574. {
  4575. struct map_session_data *sd;
  4576. int itemid;
  4577. struct item_data *item=NULL;
  4578. char output[100];
  4579. sd=va_arg(ap,struct map_session_data *);
  4580. itemid=va_arg(ap,int);
  4581. if((item=itemdb_exists(itemid))==NULL)
  4582. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4583. else
  4584. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4585. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4586. return 0;
  4587. }
  4588. /*==========================================
  4589. * Steal an item from bl (mob).
  4590. * Return:
  4591. * 0 = fail
  4592. * 1 = succes
  4593. *------------------------------------------*/
  4594. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4595. {
  4596. int i,itemid;
  4597. double rate;
  4598. unsigned char flag = 0;
  4599. struct status_data *sd_status, *md_status;
  4600. struct mob_data *md;
  4601. struct item tmp_item;
  4602. if(!sd || !bl || bl->type!=BL_MOB)
  4603. return 0;
  4604. md = (TBL_MOB *)bl;
  4605. 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
  4606. return 0;
  4607. sd_status= status_get_status_data(&sd->bl);
  4608. md_status= status_get_status_data(bl);
  4609. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4610. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4611. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4612. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4613. ) { //Can't steal from
  4614. md->state.steal_flag = UCHAR_MAX;
  4615. return 0;
  4616. }
  4617. // base skill success chance (percentual)
  4618. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4619. rate += sd->bonus.add_steal_rate;
  4620. if( rate < 1 )
  4621. return 0;
  4622. // Try dropping one item, in the order from first to last possible slot.
  4623. // Droprate is affected by the skill success rate.
  4624. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4625. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4626. break;
  4627. if( i == MAX_STEAL_DROP )
  4628. return 0;
  4629. itemid = md->db->dropitem[i].nameid;
  4630. memset(&tmp_item,0,sizeof(tmp_item));
  4631. tmp_item.nameid = itemid;
  4632. tmp_item.amount = 1;
  4633. tmp_item.identify = itemdb_isidentified(itemid);
  4634. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4635. //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?
  4636. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4637. if(flag) { //Failed to steal due to overweight
  4638. clif_additem(sd,0,0,flag);
  4639. return 0;
  4640. }
  4641. if(battle_config.show_steal_in_same_party)
  4642. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4643. //Logs items, Stolen from mobs [Lupus]
  4644. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4645. //A Rare Steal Global Announce by Lupus
  4646. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4647. struct item_data *i_data;
  4648. char message[128];
  4649. i_data = itemdb_search(itemid);
  4650. 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);
  4651. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4652. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4653. }
  4654. return 1;
  4655. }
  4656. /*==========================================
  4657. * Stole zeny from bl (mob)
  4658. * return
  4659. * 0 = fail
  4660. * 1 = success
  4661. *------------------------------------------*/
  4662. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4663. {
  4664. int rate,skill;
  4665. struct mob_data *md;
  4666. if(!sd || !target || target->type != BL_MOB)
  4667. return 0;
  4668. md = (TBL_MOB*)target;
  4669. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  4670. return 0;
  4671. // FIXME: This formula is either custom or outdated.
  4672. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4673. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4674. if(rnd()%1000 < rate)
  4675. {
  4676. int amount = md->level*10 + rnd()%100;
  4677. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4678. md->state.steal_coin_flag = 1;
  4679. return 1;
  4680. }
  4681. return 0;
  4682. }
  4683. /*==========================================
  4684. * Set's a player position.
  4685. * @param sd
  4686. * @param mapindex
  4687. * @param x
  4688. * @param y
  4689. * @param clrtype
  4690. * @return SETPOS_OK Success
  4691. * SETPOS_MAPINDEX Invalid map index
  4692. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4693. * SETPOS_AUTOTRADE Player is in autotrade state
  4694. *------------------------------------------*/
  4695. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4696. {
  4697. int16 m;
  4698. nullpo_retr(SETPOS_OK,sd);
  4699. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4700. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4701. return SETPOS_MAPINDEX;
  4702. }
  4703. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4704. return SETPOS_AUTOTRADE;
  4705. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4706. pc_setstand(sd, true);
  4707. pc_setrestartvalue(sd,1);
  4708. }
  4709. m = map_mapindex2mapid(mapindex);
  4710. sd->state.changemap = (sd->mapindex != mapindex);
  4711. sd->state.warping = 1;
  4712. if(sd->status.party_id && map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4713. struct party_data *p;
  4714. if((p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4715. instance_delusers(p->instance_id);
  4716. }
  4717. if( sd->state.changemap ) { // Misc map-changing settings
  4718. int i;
  4719. sd->state.pmap = sd->bl.m;
  4720. if (sd->sc.count) { // Cancel some map related stuff.
  4721. if (sd->sc.data[SC_JAILED])
  4722. return SETPOS_MAPINDEX; //You may not get out!
  4723. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4724. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4725. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4726. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4727. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4728. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4729. if (sd->sc.data[SC_KNOWLEDGE]) {
  4730. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4731. if (sce->timer != INVALID_TIMER)
  4732. delete_timer(sce->timer, status_change_timer);
  4733. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4734. }
  4735. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4736. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4737. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4738. }
  4739. for( i = 0; i < EQI_MAX; i++ ) {
  4740. if( sd->equip_index[i] >= 0 )
  4741. if( pc_isequip(sd,sd->equip_index[i]) )
  4742. pc_unequipitem(sd,sd->equip_index[i],2);
  4743. }
  4744. if (battle_config.clear_unit_onwarp&BL_PC)
  4745. skill_clear_unitgroup(&sd->bl);
  4746. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4747. guild_send_dot_remove(sd);
  4748. bg_send_dot_remove(sd);
  4749. if (sd->regen.state.gc)
  4750. sd->regen.state.gc = 0;
  4751. // make sure vending is allowed here
  4752. if (sd->state.vending && map[m].flag.novending) {
  4753. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4754. vending_closevending(sd);
  4755. }
  4756. channel_pcquit(sd,4); //quit map chan
  4757. }
  4758. if( m < 0 )
  4759. {
  4760. uint32 ip;
  4761. uint16 port;
  4762. //if can't find any map-servers, just abort setting position.
  4763. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4764. return SETPOS_NO_MAPSERVER;
  4765. if (sd->npc_id)
  4766. npc_event_dequeue(sd);
  4767. npc_script_event(sd, NPCE_LOGOUT);
  4768. //remove from map, THEN change x/y coordinates
  4769. unit_remove_map_pc(sd,clrtype);
  4770. sd->mapindex = mapindex;
  4771. sd->bl.x=x;
  4772. sd->bl.y=y;
  4773. pc_clean_skilltree(sd);
  4774. chrif_save(sd,2);
  4775. chrif_changemapserver(sd, ip, (short)port);
  4776. //Free session data from this map server [Kevin]
  4777. unit_free_pc(sd);
  4778. return SETPOS_OK;
  4779. }
  4780. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4781. {
  4782. 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);
  4783. x = y = 0; // make it random
  4784. }
  4785. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4786. int c=0;
  4787. do {
  4788. x = rnd()%(map[m].xs-2)+1;
  4789. y = rnd()%(map[m].ys-2)+1;
  4790. c++;
  4791. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4792. 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));
  4793. return SETPOS_OK; //preventing warp
  4794. //break; //allow warp anyway
  4795. }
  4796. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4797. }
  4798. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4799. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4800. vending_closevending(sd);
  4801. }
  4802. if(sd->bl.prev != NULL){
  4803. unit_remove_map_pc(sd,clrtype);
  4804. clif_changemap(sd,m,x,y); // [MouseJstr]
  4805. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4806. sd->state.rewarp = 1;
  4807. sd->mapindex = mapindex;
  4808. sd->bl.m = m;
  4809. sd->bl.x = sd->ud.to_x = x;
  4810. sd->bl.y = sd->ud.to_y = y;
  4811. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4812. { // Increased guild castle regen [Valaris]
  4813. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4814. if(gc && gc->guild_id == sd->status.guild_id)
  4815. sd->regen.state.gc = 1;
  4816. }
  4817. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4818. {
  4819. sd->pd->bl.m = m;
  4820. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4821. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4822. sd->pd->ud.dir = sd->ud.dir;
  4823. }
  4824. if( hom_is_active(sd->hd) )
  4825. {
  4826. sd->hd->bl.m = m;
  4827. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4828. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4829. sd->hd->ud.dir = sd->ud.dir;
  4830. }
  4831. if( sd->md )
  4832. {
  4833. sd->md->bl.m = m;
  4834. sd->md->bl.x = sd->md->ud.to_x = x;
  4835. sd->md->bl.y = sd->md->ud.to_y = y;
  4836. sd->md->ud.dir = sd->ud.dir;
  4837. }
  4838. if( sd->ed ) {
  4839. sd->ed->bl.m = m;
  4840. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4841. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4842. sd->ed->ud.dir = sd->ud.dir;
  4843. }
  4844. pc_cell_basilica(sd);
  4845. //check if we gonna be rewarped [lighta]
  4846. if(npc_check_areanpc(1,m,x,y,0)){
  4847. sd->count_rewarp++;
  4848. }
  4849. else
  4850. sd->count_rewarp = 0;
  4851. return SETPOS_OK;
  4852. }
  4853. /*==========================================
  4854. * Warp player sd to random location on current map.
  4855. * May fail if no walkable cell found (1000 attempts).
  4856. * Return:
  4857. * 0 = Success
  4858. * 1,2,3 = Fail
  4859. *------------------------------------------*/
  4860. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4861. {
  4862. int x,y,i=0;
  4863. int16 m;
  4864. nullpo_ret(sd);
  4865. m=sd->bl.m;
  4866. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4867. return 3;
  4868. do {
  4869. x = rnd()%(map[m].xs-2)+1;
  4870. y = rnd()%(map[m].ys-2)+1;
  4871. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  4872. if (i < 1000)
  4873. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4874. return 3;
  4875. }
  4876. /*==========================================
  4877. * Records a memo point at sd's current position
  4878. * pos - entry to replace, (-1: shift oldest entry out)
  4879. *------------------------------------------*/
  4880. bool pc_memo(struct map_session_data* sd, int pos)
  4881. {
  4882. int skill;
  4883. nullpo_ret(sd);
  4884. // check mapflags
  4885. 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) ) {
  4886. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4887. return false;
  4888. }
  4889. // check inputs
  4890. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4891. return false; // invalid input
  4892. // check required skill level
  4893. skill = pc_checkskill(sd, AL_WARP);
  4894. if( skill < 1 ) {
  4895. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4896. return false;
  4897. }
  4898. if( skill < 2 || skill - 2 < pos ) {
  4899. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4900. return false;
  4901. }
  4902. if( pos == -1 )
  4903. {
  4904. uint8 i;
  4905. // prevent memo-ing the same map multiple times
  4906. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4907. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4908. pos = 0;
  4909. }
  4910. if( map[sd->bl.m].instance_id ) {
  4911. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  4912. return false;
  4913. }
  4914. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4915. sd->status.memo_point[pos].x = sd->bl.x;
  4916. sd->status.memo_point[pos].y = sd->bl.y;
  4917. clif_skill_memomessage(sd, 0);
  4918. return true;
  4919. }
  4920. //
  4921. // Skills
  4922. //
  4923. /**
  4924. * Get the skill current cooldown for player.
  4925. * (get the db base cooldown for skill + player specific cooldown)
  4926. * @param sd : player pointer
  4927. * @param id : skill id
  4928. * @param lv : skill lv
  4929. * @return player skill cooldown
  4930. */
  4931. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  4932. uint8 i;
  4933. uint16 idx = skill_get_index(skill_id);
  4934. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  4935. if (!idx) return 0;
  4936. if (skill_db[idx]->cooldown[skill_lv - 1])
  4937. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  4938. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  4939. if (i < cooldownlen) {
  4940. cooldown += sd->skillcooldown[i].val;
  4941. cooldown = max(0,cooldown);
  4942. }
  4943. return cooldown;
  4944. }
  4945. /*==========================================
  4946. * Return player sd skill_lv learned for given skill
  4947. *------------------------------------------*/
  4948. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  4949. {
  4950. uint16 idx = 0;
  4951. if (sd == NULL)
  4952. return 0;
  4953. if ((idx = skill_get_index(skill_id)) == 0) {
  4954. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4955. return 0;
  4956. }
  4957. if (SKILL_CHK_GUILD(skill_id) ) {
  4958. struct guild *g;
  4959. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4960. return guild_checkskill(g,skill_id);
  4961. return 0;
  4962. }
  4963. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  4964. }
  4965. /**
  4966. * Check if we still have the correct weapon to continue the skill (actually status)
  4967. * If not ending it
  4968. * @param sd
  4969. * @return 0:error, 1:check done
  4970. */
  4971. static void pc_checkallowskill(struct map_session_data *sd)
  4972. {
  4973. const enum sc_type scw_list[] = {
  4974. SC_TWOHANDQUICKEN,
  4975. SC_ONEHAND,
  4976. SC_AURABLADE,
  4977. SC_PARRYING,
  4978. SC_SPEARQUICKEN,
  4979. SC_ADRENALINE,
  4980. SC_ADRENALINE2,
  4981. SC_DANCING,
  4982. SC_GATLINGFEVER,
  4983. };
  4984. uint8 i;
  4985. nullpo_retv(sd);
  4986. if(!sd->sc.count)
  4987. return;
  4988. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4989. { // Skills requiring specific weapon types
  4990. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4991. continue;
  4992. if(sd->sc.data[scw_list[i]] &&
  4993. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4994. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4995. }
  4996. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4997. // Spurt requires bare hands (feet, in fact xD)
  4998. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4999. if(sd->status.shield <= 0) { // Skills requiring a shield
  5000. const enum sc_type scs_list[] = {
  5001. SC_AUTOGUARD,
  5002. SC_DEFENDER,
  5003. SC_REFLECTSHIELD,
  5004. SC_REFLECTDAMAGE
  5005. };
  5006. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5007. if(sd->sc.data[scs_list[i]])
  5008. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5009. }
  5010. }
  5011. /*==========================================
  5012. * Return equipped index of item on player sd at pos
  5013. * Return
  5014. * -1 : Nothing equipped
  5015. * idx : (this index could be used in inventory to found item_data)
  5016. *------------------------------------------*/
  5017. short pc_checkequip(struct map_session_data *sd,int pos)
  5018. {
  5019. uint8 i;
  5020. nullpo_retr(-1, sd);
  5021. for(i=0;i<EQI_MAX;i++){
  5022. if(pos & equip_bitmask[i])
  5023. return sd->equip_index[i];
  5024. }
  5025. return -1;
  5026. }
  5027. /*==========================================
  5028. * Check if sd has nameid equipped somewhere
  5029. * @sd : the player session
  5030. * @nameid : id of the item to check
  5031. * @min : : see pc.h enum equip_index from ? to @max
  5032. * @max : see pc.h enum equip_index for @min to ?
  5033. * -return true,false
  5034. *------------------------------------------*/
  5035. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5036. {
  5037. int i;
  5038. for(i = min; i < max; i++) {
  5039. if(equip_bitmask[i]) {
  5040. int idx = sd->equip_index[i];
  5041. if (sd->status.inventory[idx].nameid == nameid)
  5042. return true;
  5043. }
  5044. }
  5045. return false;
  5046. }
  5047. /*==========================================
  5048. * Convert's from the client's lame Job ID system
  5049. * to the map server's 'makes sense' system. [Skotlex]
  5050. *------------------------------------------*/
  5051. int pc_jobid2mapid(unsigned short b_class)
  5052. {
  5053. switch(b_class)
  5054. {
  5055. //Novice And 1-1 Jobs
  5056. case JOB_NOVICE: return MAPID_NOVICE;
  5057. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5058. case JOB_MAGE: return MAPID_MAGE;
  5059. case JOB_ARCHER: return MAPID_ARCHER;
  5060. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5061. case JOB_MERCHANT: return MAPID_MERCHANT;
  5062. case JOB_THIEF: return MAPID_THIEF;
  5063. case JOB_TAEKWON: return MAPID_TAEKWON;
  5064. case JOB_WEDDING: return MAPID_WEDDING;
  5065. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5066. case JOB_NINJA: return MAPID_NINJA;
  5067. case JOB_XMAS: return MAPID_XMAS;
  5068. case JOB_SUMMER: return MAPID_SUMMER;
  5069. case JOB_HANBOK: return MAPID_HANBOK;
  5070. case JOB_GANGSI: return MAPID_GANGSI;
  5071. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5072. //2-1 Jobs
  5073. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5074. case JOB_KNIGHT: return MAPID_KNIGHT;
  5075. case JOB_WIZARD: return MAPID_WIZARD;
  5076. case JOB_HUNTER: return MAPID_HUNTER;
  5077. case JOB_PRIEST: return MAPID_PRIEST;
  5078. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5079. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5080. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5081. case JOB_KAGEROU:
  5082. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5083. case JOB_REBELLION: return MAPID_REBELLION;
  5084. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5085. //2-2 Jobs
  5086. case JOB_CRUSADER: return MAPID_CRUSADER;
  5087. case JOB_SAGE: return MAPID_SAGE;
  5088. case JOB_BARD:
  5089. case JOB_DANCER: return MAPID_BARDDANCER;
  5090. case JOB_MONK: return MAPID_MONK;
  5091. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5092. case JOB_ROGUE: return MAPID_ROGUE;
  5093. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5094. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5095. //Trans Novice And Trans 1-1 Jobs
  5096. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5097. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5098. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5099. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5100. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5101. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5102. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5103. //Trans 2-1 Jobs
  5104. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5105. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5106. case JOB_SNIPER: return MAPID_SNIPER;
  5107. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5108. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5109. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5110. //Trans 2-2 Jobs
  5111. case JOB_PALADIN: return MAPID_PALADIN;
  5112. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5113. case JOB_CLOWN:
  5114. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5115. case JOB_CHAMPION: return MAPID_CHAMPION;
  5116. case JOB_CREATOR: return MAPID_CREATOR;
  5117. case JOB_STALKER: return MAPID_STALKER;
  5118. //Baby Novice And Baby 1-1 Jobs
  5119. case JOB_BABY: return MAPID_BABY;
  5120. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5121. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5122. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5123. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5124. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5125. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5126. //Baby 2-1 Jobs
  5127. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5128. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5129. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5130. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5131. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5132. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5133. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5134. //Baby 2-2 Jobs
  5135. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5136. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5137. case JOB_BABY_BARD:
  5138. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5139. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5140. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5141. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5142. //3-1 Jobs
  5143. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5144. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5145. case JOB_WARLOCK: return MAPID_WARLOCK;
  5146. case JOB_RANGER: return MAPID_RANGER;
  5147. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5148. case JOB_MECHANIC: return MAPID_MECHANIC;
  5149. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5150. //3-2 Jobs
  5151. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5152. case JOB_SORCERER: return MAPID_SORCERER;
  5153. case JOB_MINSTREL:
  5154. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5155. case JOB_SURA: return MAPID_SURA;
  5156. case JOB_GENETIC: return MAPID_GENETIC;
  5157. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5158. //Trans 3-1 Jobs
  5159. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5160. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5161. case JOB_RANGER_T: return MAPID_RANGER_T;
  5162. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5163. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5164. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5165. //Trans 3-2 Jobs
  5166. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5167. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5168. case JOB_MINSTREL_T:
  5169. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5170. case JOB_SURA_T: return MAPID_SURA_T;
  5171. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5172. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5173. //Baby 3-1 Jobs
  5174. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5175. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5176. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5177. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5178. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5179. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5180. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5181. //Baby 3-2 Jobs
  5182. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5183. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5184. case JOB_BABY_MINSTREL:
  5185. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5186. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5187. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5188. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5189. //Doram Jobs
  5190. case JOB_SUMMONER: return MAPID_SUMMONER;
  5191. default:
  5192. return -1;
  5193. }
  5194. }
  5195. //Reverts the map-style class id to the client-style one.
  5196. int pc_mapid2jobid(unsigned short class_, int sex)
  5197. {
  5198. switch(class_) {
  5199. //Novice And 1-1 Jobs
  5200. case MAPID_NOVICE: return JOB_NOVICE;
  5201. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5202. case MAPID_MAGE: return JOB_MAGE;
  5203. case MAPID_ARCHER: return JOB_ARCHER;
  5204. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5205. case MAPID_MERCHANT: return JOB_MERCHANT;
  5206. case MAPID_THIEF: return JOB_THIEF;
  5207. case MAPID_TAEKWON: return JOB_TAEKWON;
  5208. case MAPID_WEDDING: return JOB_WEDDING;
  5209. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5210. case MAPID_NINJA: return JOB_NINJA;
  5211. case MAPID_XMAS: return JOB_XMAS;
  5212. case MAPID_SUMMER: return JOB_SUMMER;
  5213. case MAPID_HANBOK: return JOB_HANBOK;
  5214. case MAPID_GANGSI: return JOB_GANGSI;
  5215. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5216. //2-1 Jobs
  5217. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5218. case MAPID_KNIGHT: return JOB_KNIGHT;
  5219. case MAPID_WIZARD: return JOB_WIZARD;
  5220. case MAPID_HUNTER: return JOB_HUNTER;
  5221. case MAPID_PRIEST: return JOB_PRIEST;
  5222. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5223. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5224. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5225. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5226. case MAPID_REBELLION: return JOB_REBELLION;
  5227. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5228. //2-2 Jobs
  5229. case MAPID_CRUSADER: return JOB_CRUSADER;
  5230. case MAPID_SAGE: return JOB_SAGE;
  5231. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5232. case MAPID_MONK: return JOB_MONK;
  5233. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5234. case MAPID_ROGUE: return JOB_ROGUE;
  5235. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5236. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5237. //Trans Novice And Trans 2-1 Jobs
  5238. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5239. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5240. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5241. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5242. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5243. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5244. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5245. //Trans 2-1 Jobs
  5246. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5247. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5248. case MAPID_SNIPER: return JOB_SNIPER;
  5249. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5250. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5251. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5252. //Trans 2-2 Jobs
  5253. case MAPID_PALADIN: return JOB_PALADIN;
  5254. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5255. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5256. case MAPID_CHAMPION: return JOB_CHAMPION;
  5257. case MAPID_CREATOR: return JOB_CREATOR;
  5258. case MAPID_STALKER: return JOB_STALKER;
  5259. //Baby Novice And Baby 1-1 Jobs
  5260. case MAPID_BABY: return JOB_BABY;
  5261. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5262. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5263. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5264. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5265. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5266. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5267. //Baby 2-1 Jobs
  5268. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5269. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5270. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5271. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5272. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5273. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5274. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5275. //Baby 2-2 Jobs
  5276. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5277. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5278. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5279. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5280. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5281. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5282. //3-1 Jobs
  5283. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5284. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5285. case MAPID_WARLOCK: return JOB_WARLOCK;
  5286. case MAPID_RANGER: return JOB_RANGER;
  5287. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5288. case MAPID_MECHANIC: return JOB_MECHANIC;
  5289. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5290. //3-2 Jobs
  5291. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5292. case MAPID_SORCERER: return JOB_SORCERER;
  5293. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5294. case MAPID_SURA: return JOB_SURA;
  5295. case MAPID_GENETIC: return JOB_GENETIC;
  5296. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5297. //Trans 3-1 Jobs
  5298. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5299. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5300. case MAPID_RANGER_T: return JOB_RANGER_T;
  5301. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5302. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5303. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5304. //Trans 3-2 Jobs
  5305. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5306. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5307. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5308. case MAPID_SURA_T: return JOB_SURA_T;
  5309. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5310. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5311. //Baby 3-1 Jobs
  5312. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5313. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5314. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5315. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5316. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5317. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5318. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5319. //Baby 3-2 Jobs
  5320. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5321. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5322. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5323. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5324. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5325. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5326. //Doram Jobs
  5327. case MAPID_SUMMONER: return JOB_SUMMONER;
  5328. default:
  5329. return -1;
  5330. }
  5331. }
  5332. /*====================================================
  5333. * This function return the name of the job (by [Yor])
  5334. *----------------------------------------------------*/
  5335. const char* job_name(int class_)
  5336. {
  5337. switch (class_) {
  5338. case JOB_NOVICE:
  5339. case JOB_SWORDMAN:
  5340. case JOB_MAGE:
  5341. case JOB_ARCHER:
  5342. case JOB_ACOLYTE:
  5343. case JOB_MERCHANT:
  5344. case JOB_THIEF:
  5345. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5346. case JOB_KNIGHT:
  5347. case JOB_PRIEST:
  5348. case JOB_WIZARD:
  5349. case JOB_BLACKSMITH:
  5350. case JOB_HUNTER:
  5351. case JOB_ASSASSIN:
  5352. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5353. case JOB_KNIGHT2:
  5354. return msg_txt(NULL,557);
  5355. case JOB_CRUSADER:
  5356. case JOB_MONK:
  5357. case JOB_SAGE:
  5358. case JOB_ROGUE:
  5359. case JOB_ALCHEMIST:
  5360. case JOB_BARD:
  5361. case JOB_DANCER:
  5362. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5363. case JOB_CRUSADER2:
  5364. return msg_txt(NULL,563);
  5365. case JOB_WEDDING:
  5366. case JOB_SUPER_NOVICE:
  5367. case JOB_GUNSLINGER:
  5368. case JOB_NINJA:
  5369. case JOB_XMAS:
  5370. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5371. case JOB_SUMMER:
  5372. return msg_txt(NULL,621);
  5373. case JOB_HANBOK:
  5374. return msg_txt(NULL,694);
  5375. case JOB_OKTOBERFEST:
  5376. return msg_txt(NULL,696);
  5377. case JOB_NOVICE_HIGH:
  5378. case JOB_SWORDMAN_HIGH:
  5379. case JOB_MAGE_HIGH:
  5380. case JOB_ARCHER_HIGH:
  5381. case JOB_ACOLYTE_HIGH:
  5382. case JOB_MERCHANT_HIGH:
  5383. case JOB_THIEF_HIGH:
  5384. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5385. case JOB_LORD_KNIGHT:
  5386. case JOB_HIGH_PRIEST:
  5387. case JOB_HIGH_WIZARD:
  5388. case JOB_WHITESMITH:
  5389. case JOB_SNIPER:
  5390. case JOB_ASSASSIN_CROSS:
  5391. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5392. case JOB_LORD_KNIGHT2:
  5393. return msg_txt(NULL,582);
  5394. case JOB_PALADIN:
  5395. case JOB_CHAMPION:
  5396. case JOB_PROFESSOR:
  5397. case JOB_STALKER:
  5398. case JOB_CREATOR:
  5399. case JOB_CLOWN:
  5400. case JOB_GYPSY:
  5401. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5402. case JOB_PALADIN2:
  5403. return msg_txt(NULL,588);
  5404. case JOB_BABY:
  5405. case JOB_BABY_SWORDMAN:
  5406. case JOB_BABY_MAGE:
  5407. case JOB_BABY_ARCHER:
  5408. case JOB_BABY_ACOLYTE:
  5409. case JOB_BABY_MERCHANT:
  5410. case JOB_BABY_THIEF:
  5411. return msg_txt(NULL,595 - JOB_BABY + class_);
  5412. case JOB_BABY_KNIGHT:
  5413. case JOB_BABY_PRIEST:
  5414. case JOB_BABY_WIZARD:
  5415. case JOB_BABY_BLACKSMITH:
  5416. case JOB_BABY_HUNTER:
  5417. case JOB_BABY_ASSASSIN:
  5418. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5419. case JOB_BABY_KNIGHT2:
  5420. return msg_txt(NULL,602);
  5421. case JOB_BABY_CRUSADER:
  5422. case JOB_BABY_MONK:
  5423. case JOB_BABY_SAGE:
  5424. case JOB_BABY_ROGUE:
  5425. case JOB_BABY_ALCHEMIST:
  5426. case JOB_BABY_BARD:
  5427. case JOB_BABY_DANCER:
  5428. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5429. case JOB_BABY_CRUSADER2:
  5430. return msg_txt(NULL,608);
  5431. case JOB_SUPER_BABY:
  5432. return msg_txt(NULL,615);
  5433. case JOB_TAEKWON:
  5434. return msg_txt(NULL,616);
  5435. case JOB_STAR_GLADIATOR:
  5436. case JOB_STAR_GLADIATOR2:
  5437. return msg_txt(NULL,617);
  5438. case JOB_SOUL_LINKER:
  5439. return msg_txt(NULL,618);
  5440. case JOB_GANGSI:
  5441. case JOB_DEATH_KNIGHT:
  5442. case JOB_DARK_COLLECTOR:
  5443. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5444. case JOB_RUNE_KNIGHT:
  5445. case JOB_WARLOCK:
  5446. case JOB_RANGER:
  5447. case JOB_ARCH_BISHOP:
  5448. case JOB_MECHANIC:
  5449. case JOB_GUILLOTINE_CROSS:
  5450. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5451. case JOB_RUNE_KNIGHT_T:
  5452. case JOB_WARLOCK_T:
  5453. case JOB_RANGER_T:
  5454. case JOB_ARCH_BISHOP_T:
  5455. case JOB_MECHANIC_T:
  5456. case JOB_GUILLOTINE_CROSS_T:
  5457. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5458. case JOB_ROYAL_GUARD:
  5459. case JOB_SORCERER:
  5460. case JOB_MINSTREL:
  5461. case JOB_WANDERER:
  5462. case JOB_SURA:
  5463. case JOB_GENETIC:
  5464. case JOB_SHADOW_CHASER:
  5465. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5466. case JOB_ROYAL_GUARD_T:
  5467. case JOB_SORCERER_T:
  5468. case JOB_MINSTREL_T:
  5469. case JOB_WANDERER_T:
  5470. case JOB_SURA_T:
  5471. case JOB_GENETIC_T:
  5472. case JOB_SHADOW_CHASER_T:
  5473. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5474. case JOB_RUNE_KNIGHT2:
  5475. case JOB_RUNE_KNIGHT_T2:
  5476. return msg_txt(NULL,625);
  5477. case JOB_ROYAL_GUARD2:
  5478. case JOB_ROYAL_GUARD_T2:
  5479. return msg_txt(NULL,631);
  5480. case JOB_RANGER2:
  5481. case JOB_RANGER_T2:
  5482. return msg_txt(NULL,627);
  5483. case JOB_MECHANIC2:
  5484. case JOB_MECHANIC_T2:
  5485. return msg_txt(NULL,629);
  5486. case JOB_BABY_RUNE:
  5487. case JOB_BABY_WARLOCK:
  5488. case JOB_BABY_RANGER:
  5489. case JOB_BABY_BISHOP:
  5490. case JOB_BABY_MECHANIC:
  5491. case JOB_BABY_CROSS:
  5492. case JOB_BABY_GUARD:
  5493. case JOB_BABY_SORCERER:
  5494. case JOB_BABY_MINSTREL:
  5495. case JOB_BABY_WANDERER:
  5496. case JOB_BABY_SURA:
  5497. case JOB_BABY_GENETIC:
  5498. case JOB_BABY_CHASER:
  5499. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5500. case JOB_BABY_RUNE2:
  5501. return msg_txt(NULL,638);
  5502. case JOB_BABY_GUARD2:
  5503. return msg_txt(NULL,644);
  5504. case JOB_BABY_RANGER2:
  5505. return msg_txt(NULL,640);
  5506. case JOB_BABY_MECHANIC2:
  5507. return msg_txt(NULL,642);
  5508. case JOB_SUPER_NOVICE_E:
  5509. case JOB_SUPER_BABY_E:
  5510. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5511. case JOB_KAGEROU:
  5512. case JOB_OBORO:
  5513. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5514. case JOB_REBELLION:
  5515. return msg_txt(NULL,695);
  5516. case JOB_SUMMONER:
  5517. return msg_txt(NULL,697);
  5518. default:
  5519. return msg_txt(NULL,655);
  5520. }
  5521. }
  5522. /*====================================================
  5523. * Timered function to make id follow a target.
  5524. * @id = bl.id (player only atm)
  5525. * target is define in sd->followtarget (bl.id)
  5526. * used by pc_follow
  5527. *----------------------------------------------------*/
  5528. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5529. {
  5530. struct map_session_data *sd;
  5531. struct block_list *tbl;
  5532. sd = map_id2sd(id);
  5533. nullpo_ret(sd);
  5534. if (sd->followtimer != tid){
  5535. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5536. sd->followtimer = INVALID_TIMER;
  5537. return 0;
  5538. }
  5539. sd->followtimer = INVALID_TIMER;
  5540. tbl = map_id2bl(sd->followtarget);
  5541. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5542. {
  5543. pc_stop_following(sd);
  5544. return 0;
  5545. }
  5546. // either player or target is currently detached from map blocks (could be teleporting),
  5547. // but still connected to this map, so we'll just increment the timer and check back later
  5548. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5549. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5550. {
  5551. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5552. if (!check_distance_bl(&sd->bl, tbl, 5))
  5553. unit_walktobl(&sd->bl, tbl, 5, 0);
  5554. } else
  5555. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5556. }
  5557. sd->followtimer = add_timer(
  5558. tick + 1000, // increase time a bit to loosen up map's load
  5559. pc_follow_timer, sd->bl.id, 0);
  5560. return 0;
  5561. }
  5562. int pc_stop_following (struct map_session_data *sd)
  5563. {
  5564. nullpo_ret(sd);
  5565. if (sd->followtimer != INVALID_TIMER) {
  5566. delete_timer(sd->followtimer,pc_follow_timer);
  5567. sd->followtimer = INVALID_TIMER;
  5568. }
  5569. sd->followtarget = -1;
  5570. sd->ud.target_to = 0;
  5571. unit_stop_walking(&sd->bl, 1);
  5572. return 0;
  5573. }
  5574. int pc_follow(struct map_session_data *sd,int target_id)
  5575. {
  5576. struct block_list *bl = map_id2bl(target_id);
  5577. if (bl == NULL /*|| bl->type != BL_PC*/)
  5578. return 1;
  5579. if (sd->followtimer != INVALID_TIMER)
  5580. pc_stop_following(sd);
  5581. sd->followtarget = target_id;
  5582. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5583. return 0;
  5584. }
  5585. int pc_checkbaselevelup(struct map_session_data *sd) {
  5586. unsigned int next = pc_nextbaseexp(sd);
  5587. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5588. return 0;
  5589. do {
  5590. sd->status.base_exp -= next;
  5591. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5592. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5593. sd->status.base_exp = next-1;
  5594. next = pc_gets_status_point(sd->status.base_level);
  5595. sd->status.base_level++;
  5596. sd->status.status_point += next;
  5597. if( pc_is_maxbaselv(sd) ){
  5598. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5599. break;
  5600. }
  5601. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5602. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5603. status_calc_pet(sd->pd,SCO_NONE);
  5604. clif_updatestatus(sd,SP_STATUSPOINT);
  5605. clif_updatestatus(sd,SP_BASELEVEL);
  5606. clif_updatestatus(sd,SP_BASEEXP);
  5607. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5608. status_calc_pc(sd,SCO_FORCE);
  5609. status_percent_heal(&sd->bl,100,100);
  5610. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5611. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5612. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5613. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5614. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5615. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5616. if (sd->state.snovice_dead_flag)
  5617. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5618. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5619. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5620. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5621. }
  5622. clif_misceffect(&sd->bl,0);
  5623. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5624. if(sd->status.party_id)
  5625. party_send_levelup(sd);
  5626. pc_baselevelchanged(sd);
  5627. return 1;
  5628. }
  5629. void pc_baselevelchanged(struct map_session_data *sd) {
  5630. uint8 i;
  5631. for( i = 0; i < EQI_MAX; i++ ) {
  5632. if( sd->equip_index[i] >= 0 ) {
  5633. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5634. pc_unequipitem(sd, sd->equip_index[i], 3);
  5635. }
  5636. }
  5637. pc_show_questinfo(sd);
  5638. }
  5639. int pc_checkjoblevelup(struct map_session_data *sd)
  5640. {
  5641. unsigned int next = pc_nextjobexp(sd);
  5642. nullpo_ret(sd);
  5643. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5644. return 0;
  5645. do {
  5646. sd->status.job_exp -= next;
  5647. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5648. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5649. sd->status.job_exp = next-1;
  5650. sd->status.job_level ++;
  5651. sd->status.skill_point ++;
  5652. if( pc_is_maxjoblv(sd) ){
  5653. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5654. break;
  5655. }
  5656. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5657. clif_updatestatus(sd,SP_JOBLEVEL);
  5658. clif_updatestatus(sd,SP_JOBEXP);
  5659. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5660. clif_updatestatus(sd,SP_SKILLPOINT);
  5661. status_calc_pc(sd,SCO_FORCE);
  5662. clif_misceffect(&sd->bl,1);
  5663. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5664. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5665. npc_script_event(sd, NPCE_JOBLVUP);
  5666. return 1;
  5667. }
  5668. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5669. * @param sd Player
  5670. * @param base_exp Base EXP before peronal bonuses
  5671. * @param job_exp Job EXP before peronal bonuses
  5672. * @param src Block list that affecting the exp calculation
  5673. */
  5674. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5675. {
  5676. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5677. if (src) {
  5678. struct status_data *status = status_get_status_data(src);
  5679. if( sd->expaddrace[status->race] )
  5680. bonus += sd->expaddrace[status->race];
  5681. if( sd->expaddrace[RC_ALL] )
  5682. bonus += sd->expaddrace[RC_ALL];
  5683. if( sd->expaddclass[status->class_] )
  5684. bonus += sd->expaddclass[status->class_];
  5685. if( sd->expaddclass[CLASS_ALL] )
  5686. bonus += sd->expaddclass[CLASS_ALL];
  5687. if (battle_config.pk_mode &&
  5688. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5689. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5690. #ifdef VIP_ENABLE
  5691. //EXP bonus for VIP player
  5692. if (src && src->type == BL_MOB && pc_isvip(sd)) {
  5693. vip_bonus_base = battle_config.vip_base_exp_increase;
  5694. vip_bonus_job = battle_config.vip_job_exp_increase;
  5695. }
  5696. #endif
  5697. }
  5698. // Give EXPBOOST for quests even if src is NULL.
  5699. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5700. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5701. if( battle_config.vip_bm_increase && pc_isvip(sd) ) // Increase Battle Manual EXP rate for VIP.
  5702. bonus += ( sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase );
  5703. }
  5704. if (*base_exp) {
  5705. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5706. *base_exp = cap_value(exp, 1, UINT_MAX);
  5707. }
  5708. // Give JEXPBOOST for quests even if src is NULL.
  5709. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5710. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5711. if (*job_exp) {
  5712. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5713. *job_exp = cap_value(exp, 1, UINT_MAX);
  5714. }
  5715. return;
  5716. }
  5717. /**
  5718. * Show EXP gained by player in percentage by @showexp
  5719. * @param sd Player
  5720. * @param base_exp Base EXP gained/loss
  5721. * @param next_base_exp Base EXP needed for next base level
  5722. * @param job_exp Job EXP gained/loss
  5723. * @param next_job_exp Job EXP needed for next job level
  5724. * @param lost True:EXP penalty, lose EXP
  5725. **/
  5726. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  5727. char output[CHAT_SIZE_MAX];
  5728. nullpo_retv(sd);
  5729. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5730. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5731. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5732. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5733. clif_disp_onlyself(sd, output, strlen(output));
  5734. }
  5735. /**
  5736. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5737. * @param sd Player
  5738. * @param src EXP source
  5739. * @param base_exp Base EXP gained
  5740. * @param base_exp Job EXP gained
  5741. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5742. * @return
  5743. **/
  5744. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5745. {
  5746. unsigned int nextb = 0, nextj = 0;
  5747. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5748. nullpo_retv(sd);
  5749. if(sd->bl.prev == NULL || pc_isdead(sd))
  5750. return;
  5751. if (!(exp_flag&2)) {
  5752. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5753. return; // no exp on pvp maps
  5754. if (sd->status.guild_id>0)
  5755. base_exp -= guild_payexp(sd,base_exp);
  5756. }
  5757. flag = ((base_exp) ? 1 : 0) |
  5758. ((job_exp) ? 2 : 0) |
  5759. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5760. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5761. if (!(exp_flag&2))
  5762. pc_calcexp(sd, &base_exp, &job_exp, src);
  5763. nextb = pc_nextbaseexp(sd);
  5764. nextj = pc_nextjobexp(sd);
  5765. if (flag&4){
  5766. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5767. base_exp = 0;
  5768. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5769. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5770. }
  5771. if (flag&8){
  5772. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5773. job_exp = 0;
  5774. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5775. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5776. }
  5777. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5778. //Note that this value should never be greater than the original
  5779. //therefore no overflow checks are needed. [Skotlex]
  5780. if (nextb > 0) {
  5781. float nextbp = (float) base_exp / (float) nextb;
  5782. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5783. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5784. }
  5785. if (nextj > 0) {
  5786. float nextjp = (float) job_exp / (float) nextj;
  5787. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5788. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5789. }
  5790. }
  5791. // Give EXP for Base Level
  5792. if (base_exp) {
  5793. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5794. sd->status.base_exp = UINT32_MAX;
  5795. else
  5796. sd->status.base_exp += base_exp;
  5797. if (!pc_checkbaselevelup(sd))
  5798. clif_updatestatus(sd,SP_BASEEXP);
  5799. }
  5800. // Give EXP for Job Level
  5801. if (job_exp) {
  5802. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5803. sd->status.job_exp = UINT32_MAX;
  5804. else
  5805. sd->status.job_exp += job_exp;
  5806. if (!pc_checkjoblevelup(sd))
  5807. clif_updatestatus(sd,SP_JOBEXP);
  5808. }
  5809. if (flag&1)
  5810. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5811. if (flag&2)
  5812. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5813. if (sd->state.showexp)
  5814. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5815. }
  5816. /**
  5817. * Lost Base/Job EXP from a player
  5818. * @param sd Player
  5819. * @param base_exp Base EXP lost
  5820. * @param job_exp Job EXP lost
  5821. **/
  5822. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  5823. nullpo_retv(sd);
  5824. if (base_exp) {
  5825. base_exp = u32min(sd->status.base_exp, base_exp);
  5826. sd->status.base_exp -= base_exp;
  5827. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  5828. clif_updatestatus(sd, SP_BASEEXP);
  5829. }
  5830. if (job_exp) {
  5831. job_exp = u32min(sd->status.job_exp, job_exp);
  5832. sd->status.job_exp -= job_exp;
  5833. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  5834. clif_updatestatus(sd, SP_JOBEXP);
  5835. }
  5836. if (sd->state.showexp)
  5837. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  5838. }
  5839. /**
  5840. * Returns max base level for this character.
  5841. * @param sd Player
  5842. * @return Max Base Level
  5843. **/
  5844. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5845. return job_info[pc_class2idx(sd->status.class_)].max_level[0];
  5846. }
  5847. /**
  5848. * Returns max job level for this character.
  5849. * @param sd Player
  5850. * @return Max Job Level
  5851. **/
  5852. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5853. return job_info[pc_class2idx(sd->status.class_)].max_level[1];
  5854. }
  5855. /**
  5856. * Check if player is reached max base level
  5857. * @param sd
  5858. * @return True if reached max level
  5859. **/
  5860. bool pc_is_maxbaselv(struct map_session_data *sd) {
  5861. nullpo_retr(false, sd);
  5862. return (sd->status.base_level >= pc_maxbaselv(sd));
  5863. }
  5864. /**
  5865. * Check if player is reached max base level
  5866. * @param sd
  5867. * @return True if reached max level
  5868. **/
  5869. bool pc_is_maxjoblv(struct map_session_data *sd) {
  5870. nullpo_retr(false, sd);
  5871. return (sd->status.job_level >= pc_maxjoblv(sd));
  5872. }
  5873. /**
  5874. * Base exp needed for player to level up.
  5875. * @param sd
  5876. * @return Base EXP needed for next base level
  5877. **/
  5878. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  5879. nullpo_ret(sd);
  5880. if (sd->status.base_level == 0) // Is this something that possible?
  5881. return 0;
  5882. if (pc_is_maxbaselv(sd))
  5883. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  5884. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  5885. }
  5886. /**
  5887. * Job exp needed for player to level up.
  5888. * @param sd
  5889. * @return Job EXP needed for next job level
  5890. **/
  5891. unsigned int pc_nextjobexp(struct map_session_data *sd){
  5892. nullpo_ret(sd);
  5893. if (sd->status.job_level == 0) // Is this something that possible?
  5894. return 0;
  5895. if (pc_is_maxjoblv(sd))
  5896. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  5897. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  5898. }
  5899. /// Returns the value of the specified stat.
  5900. static int pc_getstat(struct map_session_data* sd, int type)
  5901. {
  5902. nullpo_retr(-1, sd);
  5903. switch( type ) {
  5904. case SP_STR: return sd->status.str;
  5905. case SP_AGI: return sd->status.agi;
  5906. case SP_VIT: return sd->status.vit;
  5907. case SP_INT: return sd->status.int_;
  5908. case SP_DEX: return sd->status.dex;
  5909. case SP_LUK: return sd->status.luk;
  5910. default:
  5911. return -1;
  5912. }
  5913. }
  5914. /// Sets the specified stat to the specified value.
  5915. /// Returns the new value.
  5916. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5917. {
  5918. nullpo_retr(-1, sd);
  5919. switch( type ) {
  5920. case SP_STR: sd->status.str = val; break;
  5921. case SP_AGI: sd->status.agi = val; break;
  5922. case SP_VIT: sd->status.vit = val; break;
  5923. case SP_INT: sd->status.int_ = val; break;
  5924. case SP_DEX: sd->status.dex = val; break;
  5925. case SP_LUK: sd->status.luk = val; break;
  5926. default:
  5927. return -1;
  5928. }
  5929. return val;
  5930. }
  5931. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5932. int pc_gets_status_point(int level)
  5933. {
  5934. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5935. return (statp[level+1] - statp[level]);
  5936. else //Default increase
  5937. return ((level+15) / 5);
  5938. }
  5939. #ifdef RENEWAL_STAT
  5940. /// Renewal status point cost formula
  5941. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  5942. #else
  5943. /// Pre-Renewal status point cost formula
  5944. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  5945. #endif
  5946. /// Returns the number of stat points needed to change the specified stat by val.
  5947. /// If val is negative, returns the number of stat points that would be needed to
  5948. /// raise the specified stat from (current value - val) to current value.
  5949. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5950. {
  5951. int low, high, sp = 0, max = 0;
  5952. if ( val == 0 )
  5953. return 0;
  5954. low = pc_getstat(sd,type);
  5955. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  5956. if ( low >= max && val > 0 )
  5957. return 0; // Official servers show '0' when max is reached
  5958. high = low + val;
  5959. if ( val < 0 )
  5960. swap(low, high);
  5961. for ( ; low < high; low++ )
  5962. sp += PC_STATUS_POINT_COST(low);
  5963. return sp;
  5964. }
  5965. /**
  5966. * Returns the value the specified stat can be increased by with the current
  5967. * amount of available status points for the current character's class.
  5968. *
  5969. * @param sd The target character.
  5970. * @param type Stat to verify.
  5971. * @return Maximum value the stat could grow by.
  5972. */
  5973. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  5974. {
  5975. int base, final_val, status_points, max_param;
  5976. nullpo_ret(sd);
  5977. base = final_val = pc_getstat(sd, type);
  5978. status_points = sd->status.status_point;
  5979. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  5980. while (final_val <= max_param && status_points >= 0) {
  5981. status_points -= PC_STATUS_POINT_COST(final_val);
  5982. final_val++;
  5983. }
  5984. final_val--;
  5985. return (final_val > base ? final_val-base : 0);
  5986. }
  5987. /**
  5988. * Raises a stat by the specified amount.
  5989. *
  5990. * Obeys max_parameter limits.
  5991. * Subtracts status points according to the cost of the increased stat points.
  5992. *
  5993. * @param sd The target character.
  5994. * @param type The stat to change (see enum _sp)
  5995. * @param increase The stat increase (strictly positive) amount.
  5996. * @retval true if the stat was increased by any amount.
  5997. * @retval false if there were no changes.
  5998. */
  5999. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6000. {
  6001. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6002. nullpo_ret(sd);
  6003. // check conditions
  6004. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6005. clif_statusupack(sd, type, 0, 0);
  6006. return false;
  6007. }
  6008. // check limits
  6009. current = pc_getstat(sd, type);
  6010. max_increase = pc_maxparameterincrease(sd, type);
  6011. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6012. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6013. clif_statusupack(sd, type, 0, 0);
  6014. return false;
  6015. }
  6016. // check status points
  6017. needed_points = pc_need_status_point(sd, type, increase);
  6018. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6019. clif_statusupack(sd, type, 0, 0);
  6020. return false;
  6021. }
  6022. // set new values
  6023. final_value = pc_setstat(sd, type, current + increase);
  6024. sd->status.status_point -= needed_points;
  6025. status_calc_pc(sd,SCO_NONE);
  6026. // update increase cost indicator
  6027. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6028. // update statpoint count
  6029. clif_updatestatus(sd, SP_STATUSPOINT);
  6030. // update stat value
  6031. clif_statusupack(sd, type, 1, final_value); // required
  6032. if( final_value > 255 )
  6033. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6034. return true;
  6035. }
  6036. /**
  6037. * Raises a stat by the specified amount.
  6038. *
  6039. * Obeys max_parameter limits.
  6040. * Does not subtract status points for the cost of the modified stat points.
  6041. *
  6042. * @param sd The target character.
  6043. * @param type The stat to change (see enum _sp)
  6044. * @param val The stat increase (or decrease) amount.
  6045. * @return the stat increase amount.
  6046. * @retval 0 if no changes were made.
  6047. */
  6048. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6049. {
  6050. int max, need;
  6051. nullpo_ret(sd);
  6052. if( type < SP_STR || type > SP_LUK )
  6053. {
  6054. clif_statusupack(sd,type,0,0);
  6055. return 0;
  6056. }
  6057. need = pc_need_status_point(sd,type,1);
  6058. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6059. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6060. status_calc_pc(sd,SCO_NONE);
  6061. // update increase cost indicator
  6062. if( need != pc_need_status_point(sd,type,1) )
  6063. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6064. // update stat value
  6065. clif_statusupack(sd,type,1,val); // required
  6066. if( val > 255 )
  6067. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6068. return val;
  6069. }
  6070. /*==========================================
  6071. * Update skill_lv for player sd
  6072. * Skill point allocation
  6073. *------------------------------------------*/
  6074. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6075. {
  6076. uint16 idx = skill_get_index(skill_id);
  6077. nullpo_retv(sd);
  6078. if (!idx) {
  6079. if (skill_id)
  6080. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6081. return;
  6082. }
  6083. // Level up guild skill
  6084. if (SKILL_CHK_GUILD(skill_id)) {
  6085. guild_skillup(sd, skill_id);
  6086. return;
  6087. }
  6088. // Level up homunculus skill
  6089. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6090. hom_skillup(sd->hd, skill_id);
  6091. return;
  6092. }
  6093. else {
  6094. if( sd->status.skill_point > 0 &&
  6095. sd->status.skill[idx].id &&
  6096. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6097. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6098. {
  6099. int lv, range, upgradable;
  6100. sd->status.skill[idx].lv++;
  6101. sd->status.skill_point--;
  6102. if( !skill_get_inf(skill_id) )
  6103. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6104. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6105. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6106. else
  6107. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6108. lv = sd->status.skill[idx].lv;
  6109. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6110. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6111. clif_skillup(sd,skill_id,lv,range,upgradable);
  6112. clif_updatestatus(sd,SP_SKILLPOINT);
  6113. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6114. clif_updatestatus(sd,SP_CARTINFO);
  6115. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6116. clif_skillinfoblock(sd);
  6117. }
  6118. //else
  6119. // 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);
  6120. }
  6121. }
  6122. /*==========================================
  6123. * /allskill
  6124. *------------------------------------------*/
  6125. int pc_allskillup(struct map_session_data *sd)
  6126. {
  6127. int i;
  6128. nullpo_ret(sd);
  6129. for (i = 0; i < MAX_SKILL; i++) {
  6130. 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) {
  6131. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6132. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6133. if (sd->status.skill[i].lv == 0)
  6134. sd->status.skill[i].id = 0;
  6135. }
  6136. }
  6137. if (!pc_grant_allskills(sd, true)) {
  6138. uint16 sk_id;
  6139. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].id) > 0;i++){
  6140. int inf2 = 0;
  6141. uint16 sk_idx = 0;
  6142. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6143. continue;
  6144. inf2 = skill_get_inf2(sk_id);
  6145. if (
  6146. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6147. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6148. sk_id == SG_DEVIL
  6149. )
  6150. continue; //Cannot be learned normally.
  6151. sd->status.skill[sk_idx].id = sk_id;
  6152. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6153. }
  6154. }
  6155. status_calc_pc(sd,SCO_NONE);
  6156. //Required because if you could level up all skills previously,
  6157. //the update will not be sent as only the lv variable changes.
  6158. clif_skillinfoblock(sd);
  6159. return 0;
  6160. }
  6161. /*==========================================
  6162. * /resetlvl
  6163. *------------------------------------------*/
  6164. int pc_resetlvl(struct map_session_data* sd,int type)
  6165. {
  6166. int i;
  6167. nullpo_ret(sd);
  6168. if (type != 3) //Also reset skills
  6169. pc_resetskill(sd, 0);
  6170. if(type == 1){
  6171. sd->status.skill_point=0;
  6172. sd->status.base_level=1;
  6173. sd->status.job_level=1;
  6174. sd->status.base_exp=0;
  6175. sd->status.job_exp=0;
  6176. if(sd->sc.option !=0)
  6177. sd->sc.option = 0;
  6178. sd->status.str=1;
  6179. sd->status.agi=1;
  6180. sd->status.vit=1;
  6181. sd->status.int_=1;
  6182. sd->status.dex=1;
  6183. sd->status.luk=1;
  6184. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6185. sd->status.status_point=100; // not 88 [celest]
  6186. // give platinum skills upon changing
  6187. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6188. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6189. }
  6190. }
  6191. if(type == 2){
  6192. sd->status.skill_point=0;
  6193. sd->status.base_level=1;
  6194. sd->status.job_level=1;
  6195. sd->status.base_exp=0;
  6196. sd->status.job_exp=0;
  6197. }
  6198. if(type == 3){
  6199. sd->status.base_level=1;
  6200. sd->status.base_exp=0;
  6201. }
  6202. if(type == 4){
  6203. sd->status.job_level=1;
  6204. sd->status.job_exp=0;
  6205. }
  6206. clif_updatestatus(sd,SP_STATUSPOINT);
  6207. clif_updatestatus(sd,SP_STR);
  6208. clif_updatestatus(sd,SP_AGI);
  6209. clif_updatestatus(sd,SP_VIT);
  6210. clif_updatestatus(sd,SP_INT);
  6211. clif_updatestatus(sd,SP_DEX);
  6212. clif_updatestatus(sd,SP_LUK);
  6213. clif_updatestatus(sd,SP_BASELEVEL);
  6214. clif_updatestatus(sd,SP_JOBLEVEL);
  6215. clif_updatestatus(sd,SP_STATUSPOINT);
  6216. clif_updatestatus(sd,SP_BASEEXP);
  6217. clif_updatestatus(sd,SP_JOBEXP);
  6218. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6219. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6220. clif_updatestatus(sd,SP_SKILLPOINT);
  6221. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6222. clif_updatestatus(sd,SP_UAGI);
  6223. clif_updatestatus(sd,SP_UVIT);
  6224. clif_updatestatus(sd,SP_UINT);
  6225. clif_updatestatus(sd,SP_UDEX);
  6226. clif_updatestatus(sd,SP_ULUK); // End Addition
  6227. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6228. if(sd->equip_index[i] >= 0)
  6229. if(pc_isequip(sd,sd->equip_index[i]))
  6230. pc_unequipitem(sd,sd->equip_index[i],2);
  6231. }
  6232. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6233. party_send_levelup(sd);
  6234. status_calc_pc(sd, SCO_FORCE);
  6235. clif_skillinfoblock(sd);
  6236. return 0;
  6237. }
  6238. /*==========================================
  6239. * /resetstate
  6240. *------------------------------------------*/
  6241. int pc_resetstate(struct map_session_data* sd)
  6242. {
  6243. nullpo_ret(sd);
  6244. if (battle_config.use_statpoint_table)
  6245. { // New statpoint table used here - Dexity
  6246. if (sd->status.base_level > MAX_LEVEL)
  6247. { //statp[] goes out of bounds, can't reset!
  6248. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6249. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6250. return 0;
  6251. }
  6252. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6253. }
  6254. else
  6255. {
  6256. int add=0;
  6257. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6258. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6259. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6260. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6261. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6262. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6263. sd->status.status_point+=add;
  6264. }
  6265. pc_setstat(sd, SP_STR, 1);
  6266. pc_setstat(sd, SP_AGI, 1);
  6267. pc_setstat(sd, SP_VIT, 1);
  6268. pc_setstat(sd, SP_INT, 1);
  6269. pc_setstat(sd, SP_DEX, 1);
  6270. pc_setstat(sd, SP_LUK, 1);
  6271. clif_updatestatus(sd,SP_STR);
  6272. clif_updatestatus(sd,SP_AGI);
  6273. clif_updatestatus(sd,SP_VIT);
  6274. clif_updatestatus(sd,SP_INT);
  6275. clif_updatestatus(sd,SP_DEX);
  6276. clif_updatestatus(sd,SP_LUK);
  6277. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6278. clif_updatestatus(sd,SP_UAGI);
  6279. clif_updatestatus(sd,SP_UVIT);
  6280. clif_updatestatus(sd,SP_UINT);
  6281. clif_updatestatus(sd,SP_UDEX);
  6282. clif_updatestatus(sd,SP_ULUK); // End Addition
  6283. clif_updatestatus(sd,SP_STATUSPOINT);
  6284. if( sd->mission_mobid ) { //bugreport:2200
  6285. sd->mission_mobid = 0;
  6286. sd->mission_count = 0;
  6287. pc_setglobalreg(sd, add_str("TK_MISSION_ID"), 0);
  6288. }
  6289. status_calc_pc(sd, SCO_NONE);
  6290. return 1;
  6291. }
  6292. /*==========================================
  6293. * /resetskill
  6294. * if flag&1, perform block resync and status_calc call.
  6295. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6296. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6297. *------------------------------------------*/
  6298. int pc_resetskill(struct map_session_data* sd, int flag)
  6299. {
  6300. int i, skill_point=0;
  6301. nullpo_ret(sd);
  6302. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6303. return 0;
  6304. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6305. /**
  6306. * 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)
  6307. **/
  6308. if( pc_is_taekwon_ranker(sd) )
  6309. return 0;
  6310. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6311. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6312. i = sd->sc.option;
  6313. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6314. i &= ~OPTION_RIDING;
  6315. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6316. i &= ~OPTION_FALCON;
  6317. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6318. i &= ~OPTION_DRAGON;
  6319. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6320. i &= ~OPTION_WUG;
  6321. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6322. i &= ~OPTION_WUGRIDER;
  6323. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6324. i &= ~OPTION_MADOGEAR;
  6325. #ifndef NEW_CARTS
  6326. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6327. i &= ~OPTION_CART;
  6328. #else
  6329. if( sd->sc.data[SC_PUSH_CART] )
  6330. pc_setcart(sd, 0);
  6331. #endif
  6332. if( i != sd->sc.option )
  6333. pc_setoption(sd, i);
  6334. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6335. hom_vaporize(sd, HOM_ST_ACTIVE);
  6336. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6337. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6338. }
  6339. for( i = 1; i < MAX_SKILL; i++ )
  6340. {
  6341. uint8 lv = sd->status.skill[i].lv;
  6342. int inf2;
  6343. uint16 skill_id = skill_idx2id(i);
  6344. if (lv == 0 || skill_id == 0)
  6345. continue;
  6346. inf2 = skill_get_inf2(skill_id);
  6347. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6348. continue;
  6349. // Don't reset trick dead if not a novice/baby
  6350. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6351. {
  6352. sd->status.skill[i].lv = 0;
  6353. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6354. continue;
  6355. }
  6356. // do not reset basic skill
  6357. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6358. continue;
  6359. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6360. continue;
  6361. if( flag&4 && !skill_ischangesex(skill_id) )
  6362. continue;
  6363. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6364. { //Only handle quest skills in a special way when you can't learn them manually
  6365. if( battle_config.quest_skill_reset && !(flag&2) )
  6366. { //Wipe them
  6367. sd->status.skill[i].lv = 0;
  6368. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6369. }
  6370. continue;
  6371. }
  6372. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6373. skill_point += lv;
  6374. else
  6375. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6376. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6377. if( !(flag&2) )
  6378. {// reset
  6379. sd->status.skill[i].lv = 0;
  6380. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6381. }
  6382. }
  6383. if( flag&2 || !skill_point ) return skill_point;
  6384. sd->status.skill_point += skill_point;
  6385. if (flag&1) {
  6386. clif_updatestatus(sd,SP_SKILLPOINT);
  6387. clif_skillinfoblock(sd);
  6388. status_calc_pc(sd, SCO_FORCE);
  6389. }
  6390. return skill_point;
  6391. }
  6392. /*==========================================
  6393. * /resetfeel [Komurka]
  6394. *------------------------------------------*/
  6395. int pc_resetfeel(struct map_session_data* sd)
  6396. {
  6397. int i;
  6398. nullpo_ret(sd);
  6399. for (i=0; i<MAX_PC_FEELHATE; i++)
  6400. {
  6401. sd->feel_map[i].m = -1;
  6402. sd->feel_map[i].index = 0;
  6403. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6404. }
  6405. return 0;
  6406. }
  6407. int pc_resethate(struct map_session_data* sd)
  6408. {
  6409. int i;
  6410. nullpo_ret(sd);
  6411. for (i=0; i<3; i++)
  6412. {
  6413. sd->hate_mob[i] = -1;
  6414. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6415. }
  6416. return 0;
  6417. }
  6418. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6419. {
  6420. int i, bonus = 0;
  6421. nullpo_ret(sd);
  6422. skill_id = skill_dummy2skill_id(skill_id);
  6423. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6424. if( i < ARRAYLENGTH(sd->skillatk) )
  6425. bonus = sd->skillatk[i].val;
  6426. return bonus;
  6427. }
  6428. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6429. {
  6430. int i, bonus = 0;
  6431. nullpo_ret(sd);
  6432. skill_id = skill_dummy2skill_id(skill_id);
  6433. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6434. if( i < ARRAYLENGTH(sd->subskill) )
  6435. bonus = sd->subskill[i].val;
  6436. return bonus;
  6437. }
  6438. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6439. int i, bonus = sd->bonus.add_heal_rate;
  6440. skill_id = skill_dummy2skill_id(skill_id);
  6441. if( bonus ) {
  6442. switch( skill_id ) {
  6443. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6444. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6445. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6446. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6447. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6448. }
  6449. }
  6450. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6451. if( i < ARRAYLENGTH(sd->skillheal) )
  6452. bonus += sd->skillheal[i].val;
  6453. return bonus;
  6454. }
  6455. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6456. int i, bonus = sd->bonus.add_heal2_rate;
  6457. skill_id = skill_dummy2skill_id(skill_id);
  6458. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6459. if( i < ARRAYLENGTH(sd->skillheal2) )
  6460. bonus += sd->skillheal2[i].val;
  6461. return bonus;
  6462. }
  6463. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6464. {
  6465. if( !pc_isdead(sd) )
  6466. return; // not applicable
  6467. if( sd->bg_id && bg_member_respawn(sd) )
  6468. return; // member revived by battleground
  6469. pc_setstand(sd, true);
  6470. pc_setrestartvalue(sd,3);
  6471. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6472. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6473. }
  6474. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6475. {
  6476. struct map_session_data *sd = map_id2sd(id);
  6477. if( sd != NULL )
  6478. {
  6479. sd->pvp_point=0;
  6480. pc_respawn(sd,CLR_OUTSIGHT);
  6481. }
  6482. return 0;
  6483. }
  6484. /*==========================================
  6485. * Invoked when a player has received damage
  6486. *------------------------------------------*/
  6487. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6488. {
  6489. if (sp) clif_updatestatus(sd,SP_SP);
  6490. if (hp) clif_updatestatus(sd,SP_HP);
  6491. else return;
  6492. if (!src)
  6493. return;
  6494. if( pc_issit(sd) ) {
  6495. pc_setstand(sd, true);
  6496. skill_sit(sd,0);
  6497. }
  6498. if( sd->progressbar.npc_id )
  6499. clif_progressbar_abort(sd);
  6500. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6501. pet_target_check(sd->pd,src,1);
  6502. if( sd->status.ele_id > 0 )
  6503. elemental_set_target(sd,src);
  6504. sd->canlog_tick = gettick();
  6505. }
  6506. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6507. {
  6508. TBL_PC *sd = map_id2sd(id);
  6509. if(sd) pc_close_npc(sd,data);
  6510. return 0;
  6511. }
  6512. /*
  6513. * Method to properly close npc for player and clear anything related
  6514. * @flag == 1 : produce close button
  6515. * @flag == 2 : directly close it
  6516. */
  6517. void pc_close_npc(struct map_session_data *sd,int flag)
  6518. {
  6519. nullpo_retv(sd);
  6520. if (sd->npc_id || sd->npc_shopid) {
  6521. if (sd->state.using_fake_npc) {
  6522. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6523. sd->state.using_fake_npc = 0;
  6524. }
  6525. if (sd->st) {
  6526. if(sd->st->state == RUN){ //wait ending code execution
  6527. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6528. return;
  6529. }
  6530. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6531. sd->st->mes_active = 0;
  6532. }
  6533. sd->state.menu_or_input = 0;
  6534. sd->npc_menu = 0;
  6535. sd->npc_shopid = 0;
  6536. #ifdef SECURE_NPCTIMEOUT
  6537. sd->npc_idle_timer = INVALID_TIMER;
  6538. #endif
  6539. clif_scriptclose(sd,sd->npc_id);
  6540. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6541. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6542. script_free_state(sd->st);
  6543. sd->st = NULL;
  6544. sd->npc_id = 0;
  6545. }
  6546. }
  6547. }
  6548. /*==========================================
  6549. * Invoked when a player has negative current hp
  6550. *------------------------------------------*/
  6551. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6552. {
  6553. int i=0,k=0;
  6554. unsigned int tick = gettick();
  6555. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6556. // Super Novices have no kill or die functions attached when saved by their angel
  6557. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6558. unsigned int exp = pc_nextbaseexp(sd);
  6559. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6560. sd->state.snovice_dead_flag = 1;
  6561. pc_setrestartvalue(sd,1);
  6562. status_percent_heal(&sd->bl, 100, 100);
  6563. clif_resurrection(&sd->bl, 1);
  6564. if(battle_config.pc_invincible_time)
  6565. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6566. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6567. if(map_flag_gvg(sd->bl.m))
  6568. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6569. return 0;
  6570. }
  6571. }
  6572. for(k = 0; k < MAX_DEVOTION; k++) {
  6573. if (sd->devotion[k]){
  6574. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6575. if (devsd)
  6576. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6577. sd->devotion[k] = 0;
  6578. }
  6579. }
  6580. if(sd->shadowform_id) { //if we were target of shadowform
  6581. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6582. sd->shadowform_id = 0; //should be remove on status end anyway
  6583. }
  6584. if(sd->status.pet_id > 0 && sd->pd) {
  6585. struct pet_data *pd = sd->pd;
  6586. if( !map[sd->bl.m].flag.noexppenalty ) {
  6587. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6588. if( pd->pet.intimate < 0 )
  6589. pd->pet.intimate = 0;
  6590. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6591. }
  6592. if( sd->pd->target_id ) // Unlock all targets...
  6593. pet_unlocktarget(sd->pd);
  6594. }
  6595. if (sd->status.hom_id > 0) {
  6596. if(battle_config.homunculus_auto_vapor && sd->hd)
  6597. hom_vaporize(sd, HOM_ST_ACTIVE);
  6598. }
  6599. if( sd->md )
  6600. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6601. if( sd->ed )
  6602. elemental_delete(sd->ed);
  6603. // Leave duel if you die [LuzZza]
  6604. if(battle_config.duel_autoleave_when_die) {
  6605. if(sd->duel_group > 0)
  6606. duel_leave(sd->duel_group, sd);
  6607. if(sd->duel_invite > 0)
  6608. duel_reject(sd->duel_invite, sd);
  6609. }
  6610. pc_close_npc(sd,2); //close npc if we were using one
  6611. /* e.g. not killed thru pc_damage */
  6612. if( pc_issit(sd) ) {
  6613. clif_status_load(&sd->bl,SI_SIT,0);
  6614. }
  6615. pc_setdead(sd);
  6616. pc_setglobalreg(sd, add_str("PC_DIE_COUNTER"), sd->die_counter+1);
  6617. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6618. //Reset menu skills/item skills
  6619. if ((sd->skillitem) != 0)
  6620. sd->skillitem = sd->skillitemlv = 0;
  6621. if ((sd->menuskill_id) != 0)
  6622. sd->menuskill_id = sd->menuskill_val = 0;
  6623. //Reset ticks.
  6624. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6625. if ( sd->spiritball !=0 )
  6626. pc_delspiritball(sd,sd->spiritball,0);
  6627. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6628. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6629. if (src)
  6630. switch (src->type) {
  6631. case BL_MOB:
  6632. {
  6633. struct mob_data *md=(struct mob_data *)src;
  6634. if(md->target_id==sd->bl.id)
  6635. mob_unlocktarget(md,tick);
  6636. if(battle_config.mobs_level_up && md->status.hp &&
  6637. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6638. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6639. ) { // monster level up [Valaris]
  6640. clif_misceffect(&md->bl,0);
  6641. md->level++;
  6642. status_calc_mob(md, SCO_NONE);
  6643. status_percent_heal(src,10,0);
  6644. if( battle_config.show_mob_info&4 )
  6645. {// update name with new level
  6646. clif_charnameack(0, &md->bl);
  6647. }
  6648. }
  6649. src = battle_get_master(src); // Maybe Player Summon
  6650. }
  6651. break;
  6652. case BL_PET: //Pass on to master...
  6653. case BL_HOM:
  6654. case BL_MER:
  6655. src = battle_get_master(src);
  6656. break;
  6657. }
  6658. if (src && src->type == BL_PC) {
  6659. struct map_session_data *ssd = (struct map_session_data *)src;
  6660. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6661. npc_script_event(ssd, NPCE_KILLPC);
  6662. if (battle_config.pk_mode&2) {
  6663. ssd->status.manner -= 5;
  6664. if(ssd->status.manner < 0)
  6665. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6666. #if 0
  6667. // PK/Karma system code (not enabled yet) [celest]
  6668. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6669. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6670. // karma going down = more 'good' / more honourable.
  6671. // The Karma System way...
  6672. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6673. sd->status.karma--;
  6674. ssd->status.karma--;
  6675. }
  6676. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6677. ssd->status.karma++;
  6678. // or the PK System way...
  6679. if (sd->status.karma > 0) // player killed is dishonourable?
  6680. ssd->status.karma--; // honour points earned
  6681. sd->status.karma++; // honour points lost
  6682. // To-do: Receive exp on certain occasions
  6683. #endif
  6684. }
  6685. }
  6686. if(battle_config.bone_drop==2
  6687. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6688. {
  6689. struct item item_tmp;
  6690. memset(&item_tmp,0,sizeof(item_tmp));
  6691. item_tmp.nameid=ITEMID_SKULL_;
  6692. item_tmp.identify=1;
  6693. item_tmp.card[0]=CARD0_CREATE;
  6694. item_tmp.card[1]=0;
  6695. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6696. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6697. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6698. }
  6699. //Remove bonus_script when dead
  6700. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6701. // changed penalty options, added death by player if pk_mode [Valaris]
  6702. if(battle_config.death_penalty_type
  6703. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6704. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6705. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6706. {
  6707. uint32 base_penalty = battle_config.death_penalty_base;
  6708. uint32 job_penalty = battle_config.death_penalty_job;
  6709. uint32 zeny_penalty = battle_config.zeny_penalty;
  6710. #ifdef VIP_ENABLE
  6711. if(pc_isvip(sd)){
  6712. base_penalty *= battle_config.vip_exp_penalty_base;
  6713. job_penalty *= battle_config.vip_exp_penalty_job;
  6714. }
  6715. else {
  6716. base_penalty *= battle_config.vip_exp_penalty_base_normal;
  6717. job_penalty *= battle_config.vip_exp_penalty_job_normal;
  6718. }
  6719. #endif
  6720. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6721. switch (battle_config.death_penalty_type) {
  6722. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6723. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6724. }
  6725. if (base_penalty){ //recheck after altering to speedup
  6726. if (battle_config.pk_mode && src && src->type==BL_PC)
  6727. base_penalty *= 2;
  6728. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6729. }
  6730. }
  6731. else
  6732. base_penalty = 0;
  6733. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6734. switch (battle_config.death_penalty_type) {
  6735. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6736. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6737. }
  6738. if (job_penalty) {
  6739. if (battle_config.pk_mode && src && src->type==BL_PC)
  6740. job_penalty *= 2;
  6741. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6742. }
  6743. }
  6744. else
  6745. job_penalty = 0;
  6746. if (base_penalty || job_penalty)
  6747. pc_lostexp(sd, base_penalty, job_penalty);
  6748. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6749. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6750. if(zeny_penalty)
  6751. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6752. }
  6753. }
  6754. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6755. int j;
  6756. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6757. int id = map[sd->bl.m].drop_list[j].drop_id;
  6758. int type = map[sd->bl.m].drop_list[j].drop_type;
  6759. int per = map[sd->bl.m].drop_list[j].drop_per;
  6760. if(id == 0)
  6761. continue;
  6762. if(id == -1){
  6763. int eq_num=0,eq_n[MAX_INVENTORY];
  6764. memset(eq_n,0,sizeof(eq_n));
  6765. for(i=0;i<MAX_INVENTORY;i++) {
  6766. if( (type == 1 && !sd->status.inventory[i].equip)
  6767. || (type == 2 && sd->status.inventory[i].equip)
  6768. || type == 3)
  6769. {
  6770. int l;
  6771. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6772. if( l < MAX_INVENTORY )
  6773. eq_n[l] = i;
  6774. eq_num++;
  6775. }
  6776. }
  6777. if(eq_num > 0){
  6778. int n = eq_n[rnd()%eq_num];
  6779. if(rnd()%10000 < per) {
  6780. if(sd->status.inventory[n].equip)
  6781. pc_unequipitem(sd,n,3);
  6782. pc_dropitem(sd,n,1);
  6783. }
  6784. }
  6785. }
  6786. else if(id > 0) {
  6787. for(i=0;i<MAX_INVENTORY;i++){
  6788. if(sd->status.inventory[i].nameid == id
  6789. && rnd()%10000 < per
  6790. && ((type == 1 && !sd->status.inventory[i].equip)
  6791. || (type == 2 && sd->status.inventory[i].equip)
  6792. || type == 3) ){
  6793. if(sd->status.inventory[i].equip)
  6794. pc_unequipitem(sd,i,3);
  6795. pc_dropitem(sd,i,1);
  6796. break;
  6797. }
  6798. }
  6799. }
  6800. }
  6801. }
  6802. // pvp
  6803. // disable certain pvp functions on pk_mode [Valaris]
  6804. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6805. sd->pvp_point -= 5;
  6806. sd->pvp_lost++;
  6807. if( src && src->type == BL_PC ) {
  6808. struct map_session_data *ssd = (struct map_session_data *)src;
  6809. ssd->pvp_point++;
  6810. ssd->pvp_won++;
  6811. }
  6812. if( sd->pvp_point < 0 ) {
  6813. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6814. return 1|8;
  6815. }
  6816. }
  6817. //GvG
  6818. if( map_flag_gvg(sd->bl.m) ) {
  6819. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6820. return 1|8;
  6821. }
  6822. else if( sd->bg_id ) {
  6823. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6824. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6825. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6826. return 1|8;
  6827. }
  6828. }
  6829. //Reset "can log out" tick.
  6830. if( battle_config.prevent_logout )
  6831. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6832. return 1;
  6833. }
  6834. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6835. if(hp) clif_updatestatus(sd,SP_HP);
  6836. if(sp) clif_updatestatus(sd,SP_SP);
  6837. pc_setstand(sd, true);
  6838. if(battle_config.pc_invincible_time > 0)
  6839. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6840. if( sd->state.gmaster_flag ) {
  6841. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  6842. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  6843. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  6844. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  6845. }
  6846. }
  6847. // script
  6848. //
  6849. /*==========================================
  6850. * script reading pc status registry
  6851. *------------------------------------------*/
  6852. int pc_readparam(struct map_session_data* sd,int type)
  6853. {
  6854. int val = 0;
  6855. nullpo_ret(sd);
  6856. switch(type) {
  6857. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6858. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6859. case SP_ZENY: val = sd->status.zeny; break;
  6860. case SP_BASELEVEL: val = sd->status.base_level; break;
  6861. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6862. case SP_CLASS: val = sd->status.class_; break;
  6863. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6864. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6865. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6866. case SP_SEX: val = sd->status.sex; break;
  6867. case SP_WEIGHT: val = sd->weight; break;
  6868. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6869. case SP_BASEEXP: val = sd->status.base_exp; break;
  6870. case SP_JOBEXP: val = sd->status.job_exp; break;
  6871. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6872. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6873. case SP_HP: val = sd->battle_status.hp; break;
  6874. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6875. case SP_SP: val = sd->battle_status.sp; break;
  6876. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6877. case SP_STR: val = sd->status.str; break;
  6878. case SP_AGI: val = sd->status.agi; break;
  6879. case SP_VIT: val = sd->status.vit; break;
  6880. case SP_INT: val = sd->status.int_; break;
  6881. case SP_DEX: val = sd->status.dex; break;
  6882. case SP_LUK: val = sd->status.luk; break;
  6883. case SP_KARMA: val = sd->status.karma; break;
  6884. case SP_MANNER: val = sd->status.manner; break;
  6885. case SP_FAME: val = sd->status.fame; break;
  6886. case SP_KILLERRID: val = sd->killerrid; break;
  6887. case SP_KILLEDRID: val = sd->killedrid; break;
  6888. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6889. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6890. case SP_CHARRENAME: val = sd->status.rename; break;
  6891. case SP_CHARFONT: val = sd->status.font; break;
  6892. case SP_BANK_VAULT: val = sd->bank_vault; break;
  6893. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  6894. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  6895. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  6896. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6897. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6898. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6899. case SP_DEF1: val = sd->battle_status.def; break;
  6900. case SP_DEF2: val = sd->battle_status.def2; break;
  6901. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6902. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6903. case SP_HIT: val = sd->battle_status.hit; break;
  6904. case SP_FLEE1: val = sd->battle_status.flee; break;
  6905. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6906. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6907. case SP_MAXHPRATE: val = sd->hprate; break;
  6908. case SP_MAXSPRATE: val = sd->sprate; break;
  6909. case SP_SPRATE: val = sd->dsprate; break;
  6910. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6911. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6912. case SP_ASPD_RATE:
  6913. #ifndef RENEWAL_ASPD
  6914. val = sd->battle_status.aspd_rate;
  6915. #else
  6916. val = sd->battle_status.aspd_rate2;
  6917. #endif
  6918. break;
  6919. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6920. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6921. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6922. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6923. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6924. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6925. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6926. case SP_MATK_RATE: val = sd->matk_rate; break;
  6927. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6928. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6929. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6930. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6931. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6932. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6933. case SP_HIT_RATE: val = sd->hit_rate; break;
  6934. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6935. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6936. case SP_DEF_RATE: val = sd->def_rate; break;
  6937. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6938. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6939. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6940. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6941. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6942. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6943. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6944. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6945. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6946. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6947. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6948. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6949. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6950. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6951. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6952. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6953. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6954. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6955. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6956. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6957. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6958. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6959. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6960. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6961. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6962. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6963. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6964. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6965. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6966. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6967. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6968. case SP_DELAYRATE: val = sd->delayrate; break;
  6969. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6970. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6971. case SP_UNSTRIPABLE:
  6972. case SP_UNSTRIPABLE_ARMOR:
  6973. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6974. break;
  6975. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6976. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6977. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6978. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6979. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6980. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6981. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  6982. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  6983. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  6984. case SP_EMATK: val = sd->bonus.ematk; break;
  6985. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  6986. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  6987. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  6988. case SP_CASTRATE:
  6989. case SP_VARCASTRATE:
  6990. #ifdef RENEWAL_CAST
  6991. val = sd->bonus.varcastrate; break;
  6992. #else
  6993. val = sd->castrate; break;
  6994. #endif
  6995. default:
  6996. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  6997. return -1;
  6998. }
  6999. return val;
  7000. }
  7001. /*==========================================
  7002. * script set pc status registry
  7003. *------------------------------------------*/
  7004. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7005. {
  7006. nullpo_retr(false,sd);
  7007. switch(type){
  7008. case SP_BASELEVEL:
  7009. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7010. val = pc_maxbaselv(sd);
  7011. if ((unsigned int)val > sd->status.base_level) {
  7012. int i = 0;
  7013. int stat=0;
  7014. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7015. stat += pc_gets_status_point(sd->status.base_level + i);
  7016. sd->status.status_point += stat;
  7017. }
  7018. sd->status.base_level = (unsigned int)val;
  7019. sd->status.base_exp = 0;
  7020. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7021. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7022. clif_updatestatus(sd, SP_STATUSPOINT);
  7023. clif_updatestatus(sd, SP_BASEEXP);
  7024. status_calc_pc(sd, SCO_FORCE);
  7025. if(sd->status.party_id)
  7026. party_send_levelup(sd);
  7027. break;
  7028. case SP_JOBLEVEL:
  7029. if ((unsigned int)val >= sd->status.job_level) {
  7030. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7031. sd->status.skill_point += val - sd->status.job_level;
  7032. clif_updatestatus(sd, SP_SKILLPOINT);
  7033. }
  7034. sd->status.job_level = (unsigned int)val;
  7035. sd->status.job_exp = 0;
  7036. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7037. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7038. clif_updatestatus(sd, SP_JOBEXP);
  7039. status_calc_pc(sd, SCO_FORCE);
  7040. break;
  7041. case SP_SKILLPOINT:
  7042. sd->status.skill_point = val;
  7043. break;
  7044. case SP_STATUSPOINT:
  7045. sd->status.status_point = val;
  7046. break;
  7047. case SP_ZENY:
  7048. if( val < 0 )
  7049. return false;// can't set negative zeny
  7050. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7051. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7052. break;
  7053. case SP_BASEEXP:
  7054. {
  7055. val = cap_value(val, 0, INT_MAX);
  7056. if ((unsigned int)val < sd->status.base_exp) // Lost
  7057. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7058. else // Gained
  7059. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7060. }
  7061. return true;
  7062. case SP_JOBEXP:
  7063. {
  7064. val = cap_value(val, 0, INT_MAX);
  7065. if ((unsigned int)val < sd->status.job_exp) // Lost
  7066. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7067. else // Gained
  7068. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7069. }
  7070. return true;
  7071. case SP_SEX:
  7072. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7073. break;
  7074. case SP_WEIGHT:
  7075. sd->weight = val;
  7076. break;
  7077. case SP_MAXWEIGHT:
  7078. sd->max_weight = val;
  7079. break;
  7080. case SP_HP:
  7081. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7082. break;
  7083. case SP_MAXHP:
  7084. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7085. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7086. {
  7087. sd->battle_status.hp = sd->battle_status.max_hp;
  7088. clif_updatestatus(sd, SP_HP);
  7089. }
  7090. break;
  7091. case SP_SP:
  7092. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7093. break;
  7094. case SP_MAXSP:
  7095. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7096. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7097. {
  7098. sd->battle_status.sp = sd->battle_status.max_sp;
  7099. clif_updatestatus(sd, SP_SP);
  7100. }
  7101. break;
  7102. case SP_STR:
  7103. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7104. break;
  7105. case SP_AGI:
  7106. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7107. break;
  7108. case SP_VIT:
  7109. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7110. break;
  7111. case SP_INT:
  7112. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7113. break;
  7114. case SP_DEX:
  7115. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7116. break;
  7117. case SP_LUK:
  7118. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7119. break;
  7120. case SP_KARMA:
  7121. sd->status.karma = val;
  7122. break;
  7123. case SP_MANNER:
  7124. sd->status.manner = val;
  7125. if( val < 0 )
  7126. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7127. else {
  7128. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7129. clif_manner_message(sd, 5);
  7130. }
  7131. return true; // status_change_start/status_change_end already sends packets warning the client
  7132. case SP_FAME:
  7133. sd->status.fame = val;
  7134. break;
  7135. case SP_KILLERRID:
  7136. sd->killerrid = val;
  7137. return true;
  7138. case SP_KILLEDRID:
  7139. sd->killedrid = val;
  7140. return true;
  7141. case SP_CHARMOVE:
  7142. sd->status.character_moves = val;
  7143. return true;
  7144. case SP_CHARRENAME:
  7145. sd->status.rename = val;
  7146. return true;
  7147. case SP_CHARFONT:
  7148. sd->status.font = val;
  7149. clif_font(sd);
  7150. return true;
  7151. case SP_BANK_VAULT:
  7152. if (val < 0)
  7153. return false;
  7154. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7155. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7156. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7157. return true;
  7158. case SP_ROULETTE_BRONZE:
  7159. sd->roulette_point.bronze = val;
  7160. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7161. return true;
  7162. case SP_ROULETTE_SILVER:
  7163. sd->roulette_point.silver = val;
  7164. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7165. return true;
  7166. case SP_ROULETTE_GOLD:
  7167. sd->roulette_point.gold = val;
  7168. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7169. return true;
  7170. default:
  7171. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7172. return false;
  7173. }
  7174. clif_updatestatus(sd,type);
  7175. return true;
  7176. }
  7177. /*==========================================
  7178. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7179. *------------------------------------------*/
  7180. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7181. {
  7182. if (type) {
  7183. if (hp)
  7184. clif_heal(sd->fd,SP_HP,hp);
  7185. if (sp)
  7186. clif_heal(sd->fd,SP_SP,sp);
  7187. } else {
  7188. if(hp)
  7189. clif_updatestatus(sd,SP_HP);
  7190. if(sp)
  7191. clif_updatestatus(sd,SP_SP);
  7192. }
  7193. return;
  7194. }
  7195. /*==========================================
  7196. * HP/SP Recovery
  7197. * Heal player hp and/or sp linearly.
  7198. * Calculate bonus by status.
  7199. *------------------------------------------*/
  7200. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  7201. {
  7202. int bonus, tmp, penalty = 0;
  7203. if(hp) {
  7204. int i;
  7205. bonus = 100 + (sd->battle_status.vit<<1)
  7206. + pc_checkskill(sd,SM_RECOVERY)*10
  7207. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7208. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7209. if (potion_flag > 1)
  7210. bonus += bonus*(potion_flag-1)*50/100;
  7211. //All item bonuses.
  7212. bonus += sd->bonus.itemhealrate2;
  7213. //Item Group bonuses
  7214. bonus += bonus*pc_get_itemgroup_bonus(sd, itemid)/100;
  7215. //Individual item bonuses.
  7216. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  7217. {
  7218. if (sd->itemhealrate[i].nameid == itemid) {
  7219. bonus += bonus*sd->itemhealrate[i].rate/100;
  7220. break;
  7221. }
  7222. }
  7223. tmp = hp * bonus / 100; // overflow check
  7224. if(bonus != 100 && tmp > hp)
  7225. hp = tmp;
  7226. // Recovery Potion
  7227. if( sd->sc.data[SC_INCHEALRATE] )
  7228. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  7229. // 2014 Halloween Event : Pumpkin Bonus
  7230. if(sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7231. hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100.);
  7232. }
  7233. if(sp) {
  7234. bonus = 100 + (sd->battle_status.int_<<1)
  7235. + pc_checkskill(sd,MG_SRECOVERY)*10
  7236. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7237. if (potion_flag > 1)
  7238. bonus += bonus*(potion_flag-1)*50/100;
  7239. tmp = sp * bonus / 100;
  7240. if(bonus != 100 && tmp > sp)
  7241. sp = tmp;
  7242. }
  7243. if (sd->sc.count) {
  7244. // Critical Wound and Death Hurt stack
  7245. if (sd->sc.data[SC_CRITICALWOUND])
  7246. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7247. if (sd->sc.data[SC_DEATHHURT])
  7248. penalty += 20;
  7249. if (sd->sc.data[SC_NORECOVER_STATE])
  7250. penalty = 100;
  7251. if (penalty > 0) {
  7252. hp -= hp * penalty / 100;
  7253. sp -= sp * penalty / 100;
  7254. }
  7255. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7256. hp += hp / 2; // 1.5 times
  7257. sp -= sp / 2;
  7258. }
  7259. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7260. hp += hp / 10;
  7261. sp += sp / 10;
  7262. }
  7263. #ifdef RENEWAL
  7264. if (sd->sc.data[SC_EXTREMITYFIST2])
  7265. sp = 0;
  7266. #endif
  7267. if (sd->sc.data[SC_BITESCAR])
  7268. hp = 0;
  7269. }
  7270. return status_heal(&sd->bl, hp, sp, 1);
  7271. }
  7272. /*==========================================
  7273. * HP/SP Recovery
  7274. * Heal player hp nad/or sp by rate
  7275. *------------------------------------------*/
  7276. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7277. {
  7278. nullpo_ret(sd);
  7279. if (hp > 100) hp = 100;
  7280. else if (hp <-100) hp = -100;
  7281. if (sp > 100) sp = 100;
  7282. else if (sp <-100) sp = -100;
  7283. if(hp >= 0 && sp >= 0) //Heal
  7284. return status_percent_heal(&sd->bl, hp, sp);
  7285. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7286. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7287. //Crossed signs
  7288. if(hp) {
  7289. if(hp > 0)
  7290. status_percent_heal(&sd->bl, hp, 0);
  7291. else
  7292. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7293. }
  7294. if(sp) {
  7295. if(sp > 0)
  7296. status_percent_heal(&sd->bl, 0, sp);
  7297. else
  7298. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7299. }
  7300. return 0;
  7301. }
  7302. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7303. {
  7304. struct mob_data *md;
  7305. int flag;
  7306. md = (struct mob_data *)bl;
  7307. nullpo_ret(md);
  7308. flag = va_arg(ap, int);
  7309. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7310. status_kill(&md->bl);
  7311. return 1;
  7312. }
  7313. /**
  7314. * Called when player changes job
  7315. * Rewrote to make it tidider [Celest]
  7316. * @param sd
  7317. * @param job JOB ID. See enum e_job
  7318. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7319. * @return True if success, false if failed
  7320. **/
  7321. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7322. {
  7323. int i, fame_flag = 0;
  7324. int b_class;
  7325. nullpo_retr(false,sd);
  7326. if (job < 0)
  7327. return false;
  7328. //Normalize job.
  7329. b_class = pc_jobid2mapid(job);
  7330. if (b_class == -1)
  7331. return false;
  7332. switch (upper) {
  7333. case 1:
  7334. b_class|= JOBL_UPPER;
  7335. break;
  7336. case 2:
  7337. b_class|= JOBL_BABY;
  7338. break;
  7339. }
  7340. //This will automatically adjust bard/dancer classes to the correct gender
  7341. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7342. job = pc_mapid2jobid(b_class, sd->status.sex);
  7343. if (job == -1)
  7344. return false;
  7345. if ((unsigned short)b_class == sd->class_)
  7346. return false; //Nothing to change.
  7347. // changing from 1st to 2nd job
  7348. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7349. sd->change_level_2nd = sd->status.job_level;
  7350. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  7351. }
  7352. // changing from 2nd to 3rd job
  7353. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7354. sd->change_level_3rd = sd->status.job_level;
  7355. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  7356. }
  7357. if(sd->cloneskill_idx > 0) {
  7358. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7359. sd->status.skill[sd->cloneskill_idx].id = 0;
  7360. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7361. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7362. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7363. }
  7364. sd->cloneskill_idx = 0;
  7365. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7366. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7367. }
  7368. if(sd->reproduceskill_idx > 0) {
  7369. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7370. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7371. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7372. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7373. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7374. }
  7375. sd->reproduceskill_idx = 0;
  7376. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7377. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7378. }
  7379. // Give or reduce transcendent status points
  7380. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7381. sd->status.status_point += battle_config.transcendent_status_points;
  7382. clif_updatestatus(sd,SP_STATUSPOINT);
  7383. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7384. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7385. // The player already used his bonus points, so we have to reset his status points
  7386. pc_resetstate(sd);
  7387. }
  7388. sd->status.status_point -= battle_config.transcendent_status_points;
  7389. clif_updatestatus(sd,SP_STATUSPOINT);
  7390. }
  7391. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7392. const int class_ = pc_class2idx(sd->status.class_);
  7393. short id;
  7394. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  7395. //Remove status specific to your current tree skills.
  7396. enum sc_type sc = status_skill2sc(id);
  7397. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7398. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7399. }
  7400. }
  7401. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7402. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7403. pc_resetfeel(sd);
  7404. }
  7405. // Reset body style to 0 before changing job to avoid
  7406. // errors since not every job has a alternate outfit.
  7407. sd->status.body = 0;
  7408. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7409. sd->status.class_ = job;
  7410. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7411. sd->class_ = (unsigned short)b_class;
  7412. sd->status.job_level=1;
  7413. sd->status.job_exp=0;
  7414. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7415. sd->status.base_level = pc_maxbaselv(sd);
  7416. sd->status.base_exp=0;
  7417. pc_resetstate(sd);
  7418. clif_updatestatus(sd,SP_STATUSPOINT);
  7419. clif_updatestatus(sd,SP_BASELEVEL);
  7420. clif_updatestatus(sd,SP_BASEEXP);
  7421. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7422. }
  7423. clif_updatestatus(sd,SP_JOBLEVEL);
  7424. clif_updatestatus(sd,SP_JOBEXP);
  7425. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7426. for(i=0;i<EQI_MAX;i++) {
  7427. if(sd->equip_index[i] >= 0)
  7428. if(pc_isequip(sd,sd->equip_index[i]))
  7429. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7430. }
  7431. //Change look, if disguised, you need to undisguise
  7432. //to correctly calculate new job sprite without
  7433. if (sd->disguise)
  7434. pc_disguise(sd, 0);
  7435. status_set_viewdata(&sd->bl, job);
  7436. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7437. #if PACKETVER >= 20151001
  7438. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7439. #endif
  7440. if(sd->vd.cloth_color)
  7441. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7442. /*
  7443. if(sd->vd.body_style)
  7444. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7445. */
  7446. //Update skill tree.
  7447. pc_calc_skilltree(sd);
  7448. clif_skillinfoblock(sd);
  7449. if (sd->ed)
  7450. elemental_delete(sd->ed);
  7451. if (sd->state.vending)
  7452. vending_closevending(sd);
  7453. if (sd->state.buyingstore)
  7454. buyingstore_close(sd);
  7455. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7456. //Remove peco/cart/falcon
  7457. i = sd->sc.option;
  7458. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7459. i&=~OPTION_RIDING;
  7460. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7461. i&=~OPTION_FALCON;
  7462. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7463. i&=~OPTION_DRAGON;
  7464. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7465. i&=~OPTION_WUGRIDER;
  7466. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7467. i&=~OPTION_WUG;
  7468. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7469. i&=~OPTION_MADOGEAR;
  7470. #ifndef NEW_CARTS
  7471. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7472. i&=~OPTION_CART;
  7473. #else
  7474. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7475. pc_setcart(sd, 0);
  7476. #endif
  7477. if(i != sd->sc.option)
  7478. pc_setoption(sd, i);
  7479. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7480. hom_vaporize(sd, HOM_ST_ACTIVE);
  7481. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7482. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7483. if(sd->status.manner < 0)
  7484. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7485. status_calc_pc(sd,SCO_FORCE);
  7486. pc_checkallowskill(sd);
  7487. pc_equiplookall(sd);
  7488. pc_show_questinfo(sd);
  7489. //if you were previously famous, not anymore.
  7490. if (fame_flag) {
  7491. chrif_save(sd,0);
  7492. chrif_buildfamelist();
  7493. } else if (sd->status.fame > 0) {
  7494. //It may be that now they are famous?
  7495. switch (sd->class_&MAPID_UPPERMASK) {
  7496. case MAPID_BLACKSMITH:
  7497. case MAPID_ALCHEMIST:
  7498. case MAPID_TAEKWON:
  7499. chrif_save(sd,0);
  7500. chrif_buildfamelist();
  7501. break;
  7502. }
  7503. }
  7504. return true;
  7505. }
  7506. /*==========================================
  7507. * Tell client player sd has change equipement
  7508. *------------------------------------------*/
  7509. void pc_equiplookall(struct map_session_data *sd)
  7510. {
  7511. nullpo_retv(sd);
  7512. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7513. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7514. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7515. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7516. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7517. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7518. }
  7519. /*==========================================
  7520. * Tell client player sd has change look (hair,equip...)
  7521. *------------------------------------------*/
  7522. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7523. nullpo_retv(sd);
  7524. switch(type) {
  7525. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7526. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7527. if (sd->status.hair != val) {
  7528. sd->status.hair = val;
  7529. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7530. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7531. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7532. }
  7533. break;
  7534. case LOOK_WEAPON:
  7535. sd->status.weapon = val;
  7536. break;
  7537. case LOOK_HEAD_BOTTOM:
  7538. sd->status.head_bottom = val;
  7539. break;
  7540. case LOOK_HEAD_TOP:
  7541. sd->status.head_top = val;
  7542. break;
  7543. case LOOK_HEAD_MID:
  7544. sd->status.head_mid = val;
  7545. break;
  7546. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7547. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7548. if (sd->status.hair_color != val) {
  7549. sd->status.hair_color = val;
  7550. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7551. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7552. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7553. }
  7554. break;
  7555. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7556. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7557. sd->status.clothes_color = val;
  7558. break;
  7559. case LOOK_SHIELD:
  7560. sd->status.shield = val;
  7561. break;
  7562. case LOOK_SHOES:
  7563. break;
  7564. case LOOK_ROBE:
  7565. sd->status.robe = val;
  7566. break;
  7567. case LOOK_BODY2:
  7568. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7569. sd->status.body = val;
  7570. break;
  7571. }
  7572. clif_changelook(&sd->bl, type, val);
  7573. }
  7574. /*==========================================
  7575. * Give an option (type) to player (sd) and display it to client
  7576. *------------------------------------------*/
  7577. void pc_setoption(struct map_session_data *sd,int type)
  7578. {
  7579. int p_type, new_look=0;
  7580. nullpo_retv(sd);
  7581. p_type = sd->sc.option;
  7582. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7583. sd->sc.option=type;
  7584. clif_changeoption(&sd->bl);
  7585. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7586. { // Mounting
  7587. clif_status_load(&sd->bl,SI_RIDING,1);
  7588. status_calc_pc(sd,SCO_NONE);
  7589. }
  7590. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7591. { // Dismount
  7592. clif_status_load(&sd->bl,SI_RIDING,0);
  7593. status_calc_pc(sd,SCO_NONE);
  7594. }
  7595. #ifndef NEW_CARTS
  7596. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7597. clif_cartlist(sd);
  7598. clif_updatestatus(sd, SP_CARTINFO);
  7599. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7600. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7601. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7602. clif_clearcart(sd->fd);
  7603. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7604. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7605. }
  7606. #endif
  7607. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7608. clif_status_load(&sd->bl,SI_FALCON,1);
  7609. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7610. clif_status_load(&sd->bl,SI_FALCON,0);
  7611. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7612. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7613. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7614. status_calc_pc(sd,SCO_NONE);
  7615. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7616. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7617. status_calc_pc(sd,SCO_NONE);
  7618. }
  7619. }
  7620. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7621. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7622. status_calc_pc(sd,SCO_NONE);
  7623. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7624. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7625. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7626. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7627. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7628. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7629. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7630. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7631. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7632. status_calc_pc(sd,SCO_NONE);
  7633. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7634. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7635. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7636. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7637. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7638. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7639. }
  7640. }
  7641. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7642. new_look = JOB_STAR_GLADIATOR2;
  7643. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7644. new_look = -1;
  7645. if (sd->disguise || !new_look)
  7646. return; //Disguises break sprite changes
  7647. if (new_look < 0) { //Restore normal look.
  7648. status_set_viewdata(&sd->bl, sd->status.class_);
  7649. new_look = sd->vd.class_;
  7650. }
  7651. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7652. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7653. if (sd->vd.cloth_color)
  7654. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7655. if( sd->vd.body_style )
  7656. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7657. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7658. }
  7659. /*==========================================
  7660. * Give player a cart
  7661. *------------------------------------------*/
  7662. bool pc_setcart(struct map_session_data *sd,int type) {
  7663. #ifndef NEW_CARTS
  7664. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7665. int option;
  7666. #endif
  7667. nullpo_retr(false,sd);
  7668. if( type < 0 || type > MAX_CARTS )
  7669. return false;// Never trust the values sent by the client! [Skotlex]
  7670. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7671. return false;// Push cart is required
  7672. #ifdef NEW_CARTS
  7673. switch( type ) {
  7674. case 0:
  7675. if( !sd->sc.data[SC_PUSH_CART] )
  7676. return 0;
  7677. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7678. clif_clearcart(sd->fd);
  7679. break;
  7680. default:/* everything else is an allowed ID so we can move on */
  7681. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7682. clif_cartlist(sd);
  7683. clif_updatestatus(sd, SP_CARTINFO);
  7684. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  7685. clif_status_change2(&sd->bl, sd->bl.id, AREA, SI_ON_PUSH_CART, type, 0, 0);
  7686. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  7687. sd->sc.data[SC_PUSH_CART]->val1 = type;
  7688. break;
  7689. }
  7690. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7691. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7692. #else
  7693. // Update option
  7694. option = sd->sc.option;
  7695. option &= ~OPTION_CART;// clear cart bits
  7696. option |= cart[type]; // set cart
  7697. pc_setoption(sd, option);
  7698. #endif
  7699. return true;
  7700. }
  7701. /*==========================================
  7702. * Give player a falcon
  7703. *------------------------------------------*/
  7704. void pc_setfalcon(TBL_PC* sd, int flag)
  7705. {
  7706. if( flag ){
  7707. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7708. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7709. } else if( pc_isfalcon(sd) ){
  7710. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7711. }
  7712. }
  7713. /*==========================================
  7714. * Set player riding
  7715. *------------------------------------------*/
  7716. void pc_setriding(TBL_PC* sd, int flag)
  7717. {
  7718. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7719. return;
  7720. if( flag ){
  7721. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7722. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7723. } else if( pc_isriding(sd) ){
  7724. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7725. }
  7726. }
  7727. /*==========================================
  7728. * Give player a mado
  7729. *------------------------------------------*/
  7730. void pc_setmadogear(TBL_PC* sd, int flag)
  7731. {
  7732. if( flag ){
  7733. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7734. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7735. } else if( pc_ismadogear(sd) ){
  7736. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7737. }
  7738. }
  7739. /*==========================================
  7740. * Check if player can drop an item
  7741. *------------------------------------------*/
  7742. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7743. {
  7744. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7745. return false;
  7746. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7747. return false;
  7748. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7749. }
  7750. /**
  7751. * Determines whether a player can attack based on status changes
  7752. * Why not use status_check_skilluse?
  7753. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7754. * Even ground-based attacks should be blocked by these statuses
  7755. * Called from unit_attack and unit_attack_timer_sub
  7756. * @retval true Can attack
  7757. **/
  7758. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7759. nullpo_retr(false, sd);
  7760. if (!&sd->sc)
  7761. return true;
  7762. if( sd->sc.data[SC_BASILICA] ||
  7763. sd->sc.data[SC__SHADOWFORM] ||
  7764. sd->sc.data[SC__MANHOLE] ||
  7765. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7766. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7767. sd->sc.data[SC_CRYSTALIZE] ||
  7768. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7769. sd->sc.data[SC_TRICKDEAD] ||
  7770. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7771. sd->sc.data[SC_BLADESTOP] ||
  7772. sd->sc.data[SC_DEEPSLEEP] ||
  7773. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  7774. sd->sc.data[SC_KINGS_GRACE] )
  7775. return false;
  7776. return true;
  7777. }
  7778. /*==========================================
  7779. * Read '@type' variables (temporary numeric char reg)
  7780. *------------------------------------------*/
  7781. int pc_readreg(struct map_session_data* sd, int64 reg)
  7782. {
  7783. return i64db_iget(sd->regs.vars, reg);
  7784. }
  7785. /*==========================================
  7786. * Set '@type' variables (temporary numeric char reg)
  7787. *------------------------------------------*/
  7788. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  7789. {
  7790. unsigned int index = script_getvaridx(reg);
  7791. nullpo_retr(false, sd);
  7792. if( val ) {
  7793. i64db_iput(sd->regs.vars, reg, val);
  7794. if( index )
  7795. script_array_update(&sd->regs, reg, false);
  7796. } else {
  7797. i64db_remove(sd->regs.vars, reg);
  7798. if( index )
  7799. script_array_update(&sd->regs, reg, true);
  7800. }
  7801. return true;
  7802. }
  7803. /*==========================================
  7804. * Read '@type$' variables (temporary string char reg)
  7805. *------------------------------------------*/
  7806. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  7807. {
  7808. struct script_reg_str *p = NULL;
  7809. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7810. return p ? p->value : NULL;
  7811. }
  7812. /*==========================================
  7813. * Set '@type$' variables (temporary string char reg)
  7814. *------------------------------------------*/
  7815. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  7816. {
  7817. struct script_reg_str *p = NULL;
  7818. unsigned int index = script_getvaridx(reg);
  7819. DBData prev;
  7820. nullpo_retr(false, sd);
  7821. if( str[0] ) {
  7822. p = ers_alloc(str_reg_ers, struct script_reg_str);
  7823. p->value = aStrdup(str);
  7824. p->flag.type = 1;
  7825. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7826. p = (struct script_reg_str *)db_data2ptr(&prev);
  7827. if( p->value )
  7828. aFree(p->value);
  7829. ers_free(str_reg_ers, p);
  7830. } else {
  7831. if( index )
  7832. script_array_update(&sd->regs, reg, false);
  7833. }
  7834. } else {
  7835. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  7836. p = (struct script_reg_str *)db_data2ptr(&prev);
  7837. if( p->value )
  7838. aFree(p->value);
  7839. ers_free(str_reg_ers, p);
  7840. if( index )
  7841. script_array_update(&sd->regs, reg, true);
  7842. }
  7843. }
  7844. return true;
  7845. }
  7846. /**
  7847. * Serves the following variable types:
  7848. * - 'type' (permanent numeric char reg)
  7849. * - '#type' (permanent numeric account reg)
  7850. * - '##type' (permanent numeric account reg2)
  7851. **/
  7852. int pc_readregistry(struct map_session_data *sd, int64 reg)
  7853. {
  7854. struct script_reg_num *p = NULL;
  7855. if (!sd->vars_ok) {
  7856. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7857. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7858. //intif->request_registry(sd,type==3?4:type);
  7859. set_eof(sd->fd);
  7860. return 0;
  7861. }
  7862. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  7863. return p ? p->value : 0;
  7864. }
  7865. /**
  7866. * Serves the following variable types:
  7867. * - 'type$' (permanent str char reg)
  7868. * - '#type$' (permanent str account reg)
  7869. * - '##type$' (permanent str account reg2)
  7870. **/
  7871. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  7872. {
  7873. struct script_reg_str *p = NULL;
  7874. if (!sd->vars_ok) {
  7875. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7876. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7877. //intif->request_registry(sd,type==3?4:type);
  7878. set_eof(sd->fd);
  7879. return NULL;
  7880. }
  7881. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7882. return p ? p->value : NULL;
  7883. }
  7884. /**
  7885. * Serves the following variable types:
  7886. * - 'type' (permanent numeric char reg)
  7887. * - '#type' (permanent numeric account reg)
  7888. * - '##type' (permanent numeric account reg2)
  7889. **/
  7890. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  7891. {
  7892. struct script_reg_num *p = NULL;
  7893. const char *regname = get_str(script_getvarid(reg));
  7894. unsigned int index = script_getvaridx(reg);
  7895. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  7896. switch( regname[0] ) {
  7897. default: //Char reg
  7898. if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  7899. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7900. sd->die_counter = val;
  7901. if( i )
  7902. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  7903. } else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
  7904. val = cap_value(val, 0, 1999);
  7905. sd->cook_mastery = val;
  7906. }
  7907. break;
  7908. case '#':
  7909. if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
  7910. val = cap_value(val, 0, MAX_ZENY);
  7911. sd->cashPoints = val;
  7912. } else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  7913. val = cap_value(val, 0, MAX_ZENY);
  7914. sd->kafraPoints = val;
  7915. }
  7916. break;
  7917. }
  7918. if ( !reg_load && !sd->vars_ok ) {
  7919. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  7920. return 0;
  7921. }
  7922. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  7923. if( val ) {
  7924. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  7925. script_array_update(&sd->regs, reg, false);
  7926. p->value = val;
  7927. } else {
  7928. p->value = 0;
  7929. if( index )
  7930. script_array_update(&sd->regs, reg, true);
  7931. }
  7932. if (!reg_load)
  7933. p->flag.update = 1;/* either way, it will require either delete or replace */
  7934. } else if( val ) {
  7935. DBData prev;
  7936. if( index )
  7937. script_array_update(&sd->regs, reg, false);
  7938. p = ers_alloc(num_reg_ers, struct script_reg_num);
  7939. p->value = val;
  7940. if (!reg_load)
  7941. p->flag.update = 1;
  7942. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7943. p = (struct script_reg_num *)db_data2ptr(&prev);
  7944. ers_free(num_reg_ers, p);
  7945. }
  7946. }
  7947. if (!reg_load && p)
  7948. sd->vars_dirty = true;
  7949. return 1;
  7950. }
  7951. /**
  7952. * Serves the following variable types:
  7953. * - 'type$' (permanent str char reg)
  7954. * - '#type$' (permanent str account reg)
  7955. * - '##type$' (permanent str account reg2)
  7956. **/
  7957. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  7958. {
  7959. struct script_reg_str *p = NULL;
  7960. const char *regname = get_str(script_getvarid(reg));
  7961. unsigned int index = script_getvaridx(reg);
  7962. if (!reg_load && !sd->vars_ok) {
  7963. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  7964. return 0;
  7965. }
  7966. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  7967. if( val[0] ) {
  7968. if( p->value )
  7969. aFree(p->value);
  7970. else if ( index ) // an entry that was deleted, so we reset
  7971. script_array_update(&sd->regs, reg, false);
  7972. p->value = aStrdup(val);
  7973. } else {
  7974. p->value = NULL;
  7975. if( index )
  7976. script_array_update(&sd->regs, reg, true);
  7977. }
  7978. if( !reg_load )
  7979. p->flag.update = 1; // either way, it will require either delete or replace
  7980. } else if( val[0] ) {
  7981. DBData prev;
  7982. if( index )
  7983. script_array_update(&sd->regs, reg, false);
  7984. p = ers_alloc(str_reg_ers, struct script_reg_str);
  7985. p->value = aStrdup(val);
  7986. if( !reg_load )
  7987. p->flag.update = 1;
  7988. p->flag.type = 1;
  7989. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  7990. p = (struct script_reg_str *)db_data2ptr(&prev);
  7991. if( p->value )
  7992. aFree(p->value);
  7993. ers_free(str_reg_ers, p);
  7994. }
  7995. }
  7996. if( !reg_load && p )
  7997. sd->vars_dirty = true;
  7998. return 1;
  7999. }
  8000. /**
  8001. * Set value of player variable
  8002. * @param sd Player
  8003. * @param reg Variable name
  8004. * @param value
  8005. * @return True if success, false if failed.
  8006. **/
  8007. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8008. char prefix = reg[0];
  8009. nullpo_retr(false, sd);
  8010. if (reg[strlen(reg)-1] == '$') {
  8011. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8012. return false;
  8013. }
  8014. val = cap_value(val, INT_MIN, INT_MAX);
  8015. switch (prefix) {
  8016. case '.':
  8017. case '\'':
  8018. case '$':
  8019. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8020. return false;
  8021. case '@':
  8022. return pc_setreg(sd, add_str(reg), val);
  8023. case '#':
  8024. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8025. default:
  8026. return pc_setglobalreg(sd, add_str(reg), val);
  8027. }
  8028. return false;
  8029. }
  8030. /**
  8031. * Get value of player variable
  8032. * @param sd Player
  8033. * @param reg Variable name
  8034. * @return Variable value or 0 if failed.
  8035. **/
  8036. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8037. char prefix = reg[0];
  8038. nullpo_ret(sd);
  8039. if (reg[strlen(reg)-1] == '$') {
  8040. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8041. return 0;
  8042. }
  8043. switch (prefix) {
  8044. case '.':
  8045. case '\'':
  8046. case '$':
  8047. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8048. return 0;
  8049. case '@':
  8050. return pc_readreg(sd, add_str(reg));
  8051. case '#':
  8052. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8053. default:
  8054. return pc_readglobalreg(sd, add_str(reg));
  8055. }
  8056. return 0;
  8057. }
  8058. /*==========================================
  8059. * Exec eventtimer for player sd (retrieved from map_session (id))
  8060. *------------------------------------------*/
  8061. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8062. {
  8063. struct map_session_data *sd=map_id2sd(id);
  8064. char *p = (char *)data;
  8065. int i;
  8066. if(sd==NULL)
  8067. return 0;
  8068. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8069. if( i < MAX_EVENTTIMER )
  8070. {
  8071. sd->eventtimer[i] = INVALID_TIMER;
  8072. sd->eventcount--;
  8073. npc_event(sd,p,0);
  8074. }
  8075. else
  8076. ShowError("pc_eventtimer: no such event timer\n");
  8077. if (p) aFree(p);
  8078. return 0;
  8079. }
  8080. /*==========================================
  8081. * Add eventtimer for player sd ?
  8082. *------------------------------------------*/
  8083. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8084. {
  8085. int i;
  8086. nullpo_retr(false,sd);
  8087. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8088. if( i == MAX_EVENTTIMER )
  8089. return false;
  8090. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8091. sd->eventcount++;
  8092. return true;
  8093. }
  8094. /*==========================================
  8095. * Del eventtimer for player sd ?
  8096. *------------------------------------------*/
  8097. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8098. {
  8099. char* p = NULL;
  8100. int i;
  8101. nullpo_retr(false,sd);
  8102. if (sd->eventcount == 0)
  8103. return false;
  8104. // find the named event timer
  8105. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8106. sd->eventtimer[i] != INVALID_TIMER &&
  8107. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8108. strcmp(p, name) == 0
  8109. );
  8110. if( i == MAX_EVENTTIMER )
  8111. return false; // not found
  8112. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8113. sd->eventtimer[i] = INVALID_TIMER;
  8114. if(sd->eventcount > 0)
  8115. sd->eventcount--;
  8116. aFree(p);
  8117. return true;
  8118. }
  8119. /*==========================================
  8120. * Update eventtimer count for player sd
  8121. *------------------------------------------*/
  8122. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8123. {
  8124. int i;
  8125. nullpo_retv(sd);
  8126. for(i=0;i<MAX_EVENTTIMER;i++)
  8127. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8128. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8129. addtick_timer(sd->eventtimer[i],tick);
  8130. break;
  8131. }
  8132. }
  8133. /*==========================================
  8134. * Remove all eventtimer for player sd
  8135. *------------------------------------------*/
  8136. void pc_cleareventtimer(struct map_session_data *sd)
  8137. {
  8138. int i;
  8139. nullpo_retv(sd);
  8140. if (sd->eventcount == 0)
  8141. return;
  8142. for(i=0;i<MAX_EVENTTIMER;i++){
  8143. if( sd->eventtimer[i] != INVALID_TIMER ){
  8144. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8145. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8146. sd->eventtimer[i] = INVALID_TIMER;
  8147. if(sd->eventcount > 0) //avoid looping to max val
  8148. sd->eventcount--;
  8149. if (p) aFree(p);
  8150. }
  8151. }
  8152. }
  8153. /**
  8154. * Called when an item with combo is worn
  8155. * @param *sd
  8156. * @param *data struct item_data
  8157. * @return success numbers of succeed combo
  8158. */
  8159. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8160. uint16 i;
  8161. int success = 0;
  8162. for( i = 0; i < data->combos_count; i++ ) {
  8163. struct itemchk {
  8164. int idx;
  8165. short card[MAX_SLOTS];
  8166. } *combo_idx;
  8167. int idx, j;
  8168. int nb_itemCombo;
  8169. unsigned int pos = 0;
  8170. /* ensure this isn't a duplicate combo */
  8171. if( sd->combos.bonus != NULL ) {
  8172. int x;
  8173. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8174. /* found a match, skip this combo */
  8175. if( x < sd->combos.count )
  8176. continue;
  8177. }
  8178. nb_itemCombo = data->combos[i]->count;
  8179. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8180. continue;
  8181. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8182. for(j=0; j < nb_itemCombo; j++){
  8183. combo_idx[j].idx=-1;
  8184. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8185. }
  8186. for( j = 0; j < nb_itemCombo; j++ ) {
  8187. uint16 id = data->combos[i]->nameid[j], k;
  8188. bool found = false;
  8189. for( k = 0; k < EQI_MAX; k++ ) {
  8190. short index = sd->equip_index[k];
  8191. if( index < 0 )
  8192. continue;
  8193. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8194. continue;
  8195. if (!sd->inventory_data[index] )
  8196. continue;
  8197. if ( itemdb_type(id) != IT_CARD ) {
  8198. if ( sd->inventory_data[index]->nameid != id )
  8199. continue;
  8200. if(j>0){ //check if this item not already used
  8201. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8202. uint8 z;
  8203. for (z = 0; z < nb_itemCombo-1; z++)
  8204. if(combo_idx[z].idx == index) //we already have that index recorded
  8205. do_continue=true;
  8206. if(do_continue)
  8207. continue;
  8208. }
  8209. combo_idx[j].idx = index;
  8210. pos |= sd->status.inventory[index].equip;
  8211. found = true;
  8212. break;
  8213. } else { //Cards
  8214. uint16 z;
  8215. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  8216. continue;
  8217. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  8218. bool do_continue=false;
  8219. if (sd->status.inventory[index].card[z] != id)
  8220. continue;
  8221. if(j>0){
  8222. int c1, c2;
  8223. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8224. if(combo_idx[c1].idx == index){
  8225. for (c2 = 0; c2 < sd->inventory_data[index]->slot; c2++){
  8226. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8227. do_continue = true;
  8228. break;
  8229. }
  8230. }
  8231. }
  8232. }
  8233. }
  8234. if(do_continue)
  8235. continue;
  8236. combo_idx[j].idx = index;
  8237. combo_idx[j].card[z] = id;
  8238. pos |= sd->status.inventory[index].equip;
  8239. found = true;
  8240. break;
  8241. }
  8242. }
  8243. }
  8244. if( !found )
  8245. break;/* we haven't found all the ids for this combo, so we can return */
  8246. }
  8247. aFree(combo_idx);
  8248. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8249. if( j < nb_itemCombo )
  8250. continue;
  8251. /* we got here, means all items in the combo are matching */
  8252. idx = sd->combos.count;
  8253. if( sd->combos.bonus == NULL ) {
  8254. CREATE(sd->combos.bonus, struct script_code *, 1);
  8255. CREATE(sd->combos.id, unsigned short, 1);
  8256. CREATE(sd->combos.pos, unsigned int, 1);
  8257. sd->combos.count = 1;
  8258. } else {
  8259. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8260. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8261. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8262. }
  8263. /* we simply copy the pointer */
  8264. sd->combos.bonus[idx] = data->combos[i]->script;
  8265. /* save this combo's id */
  8266. sd->combos.id[idx] = data->combos[i]->id;
  8267. /* save pos of combo*/
  8268. sd->combos.pos[idx] = pos;
  8269. success++;
  8270. }
  8271. return success;
  8272. }
  8273. /**
  8274. * Called when an item with combo is removed
  8275. * @param *sd
  8276. * @param *data struct item_data
  8277. * @return retval numbers of removed combo
  8278. */
  8279. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8280. int i, retval = 0;
  8281. if( sd->combos.bonus == NULL )
  8282. return 0;/* nothing to do here, player has no combos */
  8283. for( i = 0; i < data->combos_count; i++ ) {
  8284. /* check if this combo exists in this user */
  8285. int x = 0, cursor = 0, j;
  8286. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8287. /* no match, skip this combo */
  8288. if(x >= sd->combos.count)
  8289. continue;
  8290. sd->combos.bonus[x] = NULL;
  8291. sd->combos.id[x] = 0;
  8292. sd->combos.pos[x] = 0;
  8293. retval++;
  8294. /* check if combo requirements still fit */
  8295. if( pc_checkcombo( sd, data ) )
  8296. continue;
  8297. /* move next value to empty slot */
  8298. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8299. if( sd->combos.bonus[j] == NULL )
  8300. continue;
  8301. if( cursor != j ) {
  8302. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8303. sd->combos.id[cursor] = sd->combos.id[j];
  8304. sd->combos.pos[cursor] = sd->combos.pos[j];
  8305. }
  8306. cursor++;
  8307. }
  8308. /* it's empty, we can clear all the memory */
  8309. if( (sd->combos.count = cursor) == 0 ) {
  8310. aFree(sd->combos.bonus);
  8311. aFree(sd->combos.id);
  8312. aFree(sd->combos.pos);
  8313. sd->combos.bonus = NULL;
  8314. sd->combos.id = NULL;
  8315. sd->combos.pos = NULL;
  8316. return retval; /* we also can return at this point for we have no more combos to check */
  8317. }
  8318. }
  8319. return retval;
  8320. }
  8321. /**
  8322. * Load combo data(s) of player
  8323. * @param *sd
  8324. * @return ret numbers of succeed combo
  8325. */
  8326. int pc_load_combo(struct map_session_data *sd) {
  8327. int i, ret = 0;
  8328. for( i = 0; i < EQI_MAX; i++ ) {
  8329. struct item_data *id = NULL;
  8330. short idx = sd->equip_index[i];
  8331. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8332. continue;
  8333. if( id->combos_count )
  8334. ret += pc_checkcombo(sd,id);
  8335. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  8336. struct item_data *data;
  8337. int j;
  8338. for( j = 0; j < id->slot; j++ ) {
  8339. if (!sd->status.inventory[idx].card[j])
  8340. continue;
  8341. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  8342. if( data->combos_count )
  8343. ret += pc_checkcombo(sd,data);
  8344. }
  8345. }
  8346. }
  8347. }
  8348. return ret;
  8349. }
  8350. /*==========================================
  8351. * Equip item on player sd at req_pos from inventory index n
  8352. * return: false - fail; true - success
  8353. *------------------------------------------*/
  8354. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8355. {
  8356. int i, pos, flag = 0, iflag;
  8357. struct item_data *id;
  8358. uint8 res = ITEM_EQUIP_ACK_OK;
  8359. nullpo_retr(false,sd);
  8360. if( n < 0 || n >= MAX_INVENTORY ) {
  8361. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8362. return false;
  8363. }
  8364. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8365. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8366. return false;
  8367. }
  8368. if (!(id = sd->inventory_data[n]))
  8369. return false;
  8370. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8371. if(battle_config.battle_log)
  8372. ShowInfo("equip %hu(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  8373. if((res = pc_isequip(sd,n))) {
  8374. clif_equipitemack(sd,n,0,res); // fail
  8375. return false;
  8376. }
  8377. if (!(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  8378. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8379. return false;
  8380. }
  8381. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8382. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8383. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8384. return false;
  8385. }
  8386. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8387. /** Failing condition:
  8388. * 1. Always failed to equip ammo if no weapon equipped yet
  8389. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8390. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8391. * (4. The rest is relying on item job/class restriction).
  8392. **/
  8393. if (id->type == IT_AMMO) {
  8394. int w_idx = sd->equip_index[EQI_HAND_R];
  8395. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8396. if (w_idx == -1 ||
  8397. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8398. (id->look != A_GRENADE && w_type == W_GRENADE))
  8399. {
  8400. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8401. return false;
  8402. }
  8403. }
  8404. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8405. int a_idx = sd->equip_index[EQI_AMMO];
  8406. if (a_idx != -1) {
  8407. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8408. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8409. (a_type != A_GRENADE && id->look == W_GRENADE))
  8410. {
  8411. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8412. return false;
  8413. }
  8414. }
  8415. }
  8416. }
  8417. if (id->flag.bindOnEquip && !sd->status.inventory[n].bound) {
  8418. sd->status.inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8419. clif_notify_bindOnEquip(sd,n);
  8420. }
  8421. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8422. pos = req_pos&EQP_ACC;
  8423. if (pos == EQP_ACC) //User specified both slots..
  8424. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8425. }
  8426. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8427. pos = req_pos&EQP_SHADOW_ACC;
  8428. if (pos == EQP_SHADOW_ACC)
  8429. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8430. }
  8431. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8432. pos = (req_pos&EQP_ARMS);
  8433. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8434. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8435. }
  8436. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8437. //Update skill-block range database when weapon range changes. [Skotlex]
  8438. i = sd->equip_index[EQI_HAND_R];
  8439. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8440. flag = 1;
  8441. else
  8442. flag = id->range != sd->inventory_data[i]->range;
  8443. }
  8444. for(i=0;i<EQI_MAX;i++) {
  8445. if(pos & equip_bitmask[i]) {
  8446. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8447. pc_unequipitem(sd,sd->equip_index[i],2);
  8448. sd->equip_index[i] = n;
  8449. }
  8450. }
  8451. if(pos==EQP_AMMO) {
  8452. clif_arrowequip(sd,n);
  8453. clif_arrow_fail(sd,3);
  8454. }
  8455. else
  8456. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8457. sd->status.inventory[n].equip=pos;
  8458. if(pos & EQP_HAND_R) {
  8459. if(id)
  8460. sd->weapontype1 = id->look;
  8461. else
  8462. sd->weapontype1 = 0;
  8463. pc_calcweapontype(sd);
  8464. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8465. }
  8466. if(pos & EQP_HAND_L) {
  8467. if(id) {
  8468. if(id->type == IT_WEAPON) {
  8469. sd->status.shield = 0;
  8470. sd->weapontype2 = id->look;
  8471. }
  8472. else
  8473. if(id->type == IT_ARMOR) {
  8474. sd->status.shield = id->look;
  8475. sd->weapontype2 = 0;
  8476. }
  8477. }
  8478. else
  8479. sd->status.shield = sd->weapontype2 = 0;
  8480. pc_calcweapontype(sd);
  8481. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8482. }
  8483. //Added check to prevent sending the same look on multiple slots ->
  8484. //causes client to redraw item on top of itself. (suggested by Lupus)
  8485. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  8486. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  8487. sd->status.head_bottom = id->look;
  8488. else
  8489. sd->status.head_bottom = 0;
  8490. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8491. }
  8492. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  8493. if(id)
  8494. sd->status.head_top = id->look;
  8495. else
  8496. sd->status.head_top = 0;
  8497. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8498. }
  8499. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  8500. if(id && !(pos&EQP_HEAD_TOP))
  8501. sd->status.head_mid = id->look;
  8502. else
  8503. sd->status.head_mid = 0;
  8504. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8505. }
  8506. if(pos & EQP_COSTUME_HEAD_TOP) {
  8507. if(id){
  8508. sd->status.head_top = id->look;
  8509. } else
  8510. sd->status.head_top = 0;
  8511. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8512. }
  8513. if(pos & EQP_COSTUME_HEAD_MID) {
  8514. if(id && !(pos&EQP_HEAD_TOP)){
  8515. sd->status.head_mid = id->look;
  8516. } else
  8517. sd->status.head_mid = 0;
  8518. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8519. }
  8520. if(pos & EQP_COSTUME_HEAD_LOW) {
  8521. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  8522. sd->status.head_bottom = id->look;
  8523. } else
  8524. sd->status.head_bottom = 0;
  8525. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8526. }
  8527. if(pos & EQP_SHOES)
  8528. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8529. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8530. sd->status.robe = id ? id->look : 0;
  8531. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  8532. }
  8533. if(pos & EQP_COSTUME_GARMENT) {
  8534. sd->status.robe = id ? id->look : 0;
  8535. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8536. }
  8537. pc_checkallowskill(sd); //Check if status changes should be halted.
  8538. iflag = sd->npc_item_flag;
  8539. /* check for combos (MUST be before status_calc_pc) */
  8540. if( id->combos_count )
  8541. pc_checkcombo(sd,id);
  8542. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8543. ; //No cards
  8544. else {
  8545. for( i = 0; i < id->slot; i++ ) {
  8546. struct item_data *data;
  8547. if (!sd->status.inventory[n].card[i])
  8548. continue;
  8549. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8550. if( data->combos_count )
  8551. pc_checkcombo(sd,data);
  8552. }
  8553. }
  8554. }
  8555. status_calc_pc(sd,SCO_NONE);
  8556. if (flag) //Update skill data
  8557. clif_skillinfoblock(sd);
  8558. //OnEquip script [Skotlex]
  8559. if (id) {
  8560. //only run the script if item isn't restricted
  8561. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8562. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8563. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8564. ; //No cards
  8565. else {
  8566. for( i = 0; i < id->slot; i++ ) {
  8567. struct item_data *data;
  8568. if (!sd->status.inventory[n].card[i])
  8569. continue;
  8570. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8571. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8572. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8573. }
  8574. }
  8575. }
  8576. }
  8577. sd->npc_item_flag = iflag;
  8578. return true;
  8579. }
  8580. /*==========================================
  8581. * Called when attemting to unequip an item from player
  8582. * type:
  8583. * 0 - only unequip
  8584. * 1 - calculate status after unequipping
  8585. * 2 - force unequip
  8586. * return: false - fail; true - success
  8587. *------------------------------------------*/
  8588. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8589. int i, iflag;
  8590. bool status_cacl = false;
  8591. nullpo_retr(false,sd);
  8592. if (n < 0 || n >= MAX_INVENTORY) {
  8593. clif_unequipitemack(sd,0,0,0);
  8594. return false;
  8595. }
  8596. if (!sd->status.inventory[n].equip) {
  8597. clif_unequipitemack(sd,n,0,0);
  8598. return false; //Nothing to unequip
  8599. }
  8600. // status change that makes player cannot unequip equipment
  8601. if (!(flag&2) && sd->sc.count &&
  8602. (sd->sc.data[SC_BERSERK] ||
  8603. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8604. sd->sc.data[SC__BLOODYLUST] ||
  8605. sd->sc.data[SC_KYOUGAKU] ||
  8606. (sd->sc.data[SC_PYROCLASTIC] &&
  8607. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8608. {
  8609. clif_unequipitemack(sd,n,0,0);
  8610. return false;
  8611. }
  8612. if (battle_config.battle_log)
  8613. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  8614. for(i = 0; i < EQI_MAX; i++) {
  8615. if (sd->status.inventory[n].equip & equip_bitmask[i])
  8616. sd->equip_index[i] = -1;
  8617. }
  8618. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  8619. sd->weapontype1 = 0;
  8620. sd->status.weapon = sd->weapontype2;
  8621. pc_calcweapontype(sd);
  8622. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8623. if( !battle_config.dancing_weaponswitch_fix )
  8624. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8625. }
  8626. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  8627. sd->status.shield = sd->weapontype2 = 0;
  8628. pc_calcweapontype(sd);
  8629. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8630. }
  8631. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  8632. sd->status.head_bottom = 0;
  8633. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8634. }
  8635. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  8636. sd->status.head_top = 0;
  8637. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8638. }
  8639. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  8640. sd->status.head_mid = 0;
  8641. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8642. }
  8643. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  8644. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  8645. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8646. }
  8647. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  8648. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  8649. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8650. }
  8651. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  8652. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  8653. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8654. }
  8655. if(sd->status.inventory[n].equip & EQP_SHOES)
  8656. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8657. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8658. sd->status.robe = 0;
  8659. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  8660. }
  8661. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  8662. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  8663. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8664. }
  8665. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  8666. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8667. // On weapon change (right and left hand)
  8668. if ((sd->status.inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8669. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8670. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8671. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8672. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8673. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8674. }
  8675. // On armor change
  8676. if (sd->status.inventory[n].equip & EQP_ARMOR) {
  8677. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8678. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8679. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8680. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8681. }
  8682. // On ammo change
  8683. if (sd->inventory_data[n]->type == IT_AMMO)
  8684. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8685. if( sd->state.autobonus&sd->status.inventory[n].equip )
  8686. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  8687. sd->status.inventory[n].equip = 0;
  8688. iflag = sd->npc_item_flag;
  8689. /* check for combos (MUST be before status_calc_pc) */
  8690. if ( sd->inventory_data[n] ) {
  8691. if( sd->inventory_data[n]->combos_count ) {
  8692. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8693. status_cacl = true;
  8694. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8695. ; //No cards
  8696. else {
  8697. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8698. struct item_data *data;
  8699. if (!sd->status.inventory[n].card[i])
  8700. continue;
  8701. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8702. if( data->combos_count ) {
  8703. if( pc_removecombo(sd,data) )
  8704. status_cacl = true;
  8705. }
  8706. }
  8707. }
  8708. }
  8709. }
  8710. if(flag&1 || status_cacl) {
  8711. pc_checkallowskill(sd);
  8712. status_calc_pc(sd,SCO_NONE);
  8713. }
  8714. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8715. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8716. //OnUnEquip script [Skotlex]
  8717. if (sd->inventory_data[n]) {
  8718. if (sd->inventory_data[n]->unequip_script)
  8719. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8720. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8721. ; //No cards
  8722. else {
  8723. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8724. struct item_data *data;
  8725. if (!sd->status.inventory[n].card[i])
  8726. continue;
  8727. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8728. if( data->unequip_script )
  8729. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8730. }
  8731. }
  8732. }
  8733. }
  8734. sd->npc_item_flag = iflag;
  8735. return true;
  8736. }
  8737. /*==========================================
  8738. * Checking if player (sd) has an invalid item
  8739. * and is unequiped on map load (item_noequip)
  8740. *------------------------------------------*/
  8741. void pc_checkitem(struct map_session_data *sd) {
  8742. int i, calc_flag = 0;
  8743. struct item it;
  8744. nullpo_retv(sd);
  8745. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8746. return;
  8747. pc_check_available_item(sd); // Check for invalid(ated) items.
  8748. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8749. it = sd->status.inventory[i];
  8750. if( it.nameid == 0 )
  8751. continue;
  8752. if( !it.equip )
  8753. continue;
  8754. if( it.equip&~pc_equippoint(sd,i) ) {
  8755. pc_unequipitem(sd, i, 2);
  8756. calc_flag = 1;
  8757. continue;
  8758. }
  8759. 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) ) {
  8760. pc_unequipitem(sd, i, 2);
  8761. calc_flag = 1;
  8762. continue;
  8763. }
  8764. }
  8765. if( calc_flag && sd->state.active ) {
  8766. pc_checkallowskill(sd);
  8767. status_calc_pc(sd,SCO_NONE);
  8768. }
  8769. }
  8770. /*==========================================
  8771. * Checks for unavailable items and removes them.
  8772. *------------------------------------------*/
  8773. void pc_check_available_item(struct map_session_data *sd)
  8774. {
  8775. int i;
  8776. unsigned short nameid;
  8777. char output[256];
  8778. nullpo_retv(sd);
  8779. if (battle_config.item_check&0x1) { // Check for invalid(ated) items in inventory.
  8780. for(i = 0; i < MAX_INVENTORY; i++) {
  8781. nameid = sd->status.inventory[i].nameid;
  8782. if (!nameid)
  8783. continue;
  8784. if (!itemdb_available(nameid)) {
  8785. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8786. clif_displaymessage(sd->fd, output);
  8787. ShowWarning("Removed invalid/disabled item id %hu from inventory (amount=%d, char_id=%d).\n", nameid, sd->status.inventory[i].amount, sd->status.char_id);
  8788. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8789. continue;
  8790. }
  8791. if (!sd->status.inventory[i].unique_id && !itemdb_isstackable(nameid))
  8792. sd->status.inventory[i].unique_id = pc_generate_unique_id(sd);
  8793. }
  8794. }
  8795. if (battle_config.item_check&0x2) { // Check for invalid(ated) items in cart.
  8796. for(i = 0; i < MAX_CART; i++) {
  8797. nameid = sd->status.cart[i].nameid;
  8798. if (!nameid)
  8799. continue;
  8800. if (!itemdb_available(nameid)) {
  8801. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  8802. clif_displaymessage(sd->fd, output);
  8803. ShowWarning("Removed invalid/disabled item id %hu from cart (amount=%d, char_id=%d).\n", nameid, sd->status.cart[i].amount, sd->status.char_id);
  8804. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8805. continue;
  8806. }
  8807. if (!sd->status.cart[i].unique_id && !itemdb_isstackable(nameid))
  8808. sd->status.cart[i].unique_id = pc_generate_unique_id(sd);
  8809. }
  8810. }
  8811. if (battle_config.item_check&0x4) { // Check for invalid(ated) items in storage.
  8812. for(i = 0; i < sd->storage_size; i++) {
  8813. nameid = sd->status.storage.items[i].nameid;
  8814. if (!nameid)
  8815. continue;
  8816. if (!itemdb_available(nameid)) {
  8817. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  8818. clif_displaymessage(sd->fd, output);
  8819. ShowWarning("Removed invalid/disabled item id %hu from storage (amount=%d, char_id=%d).\n", nameid, sd->status.storage.items[i].amount, sd->status.char_id);
  8820. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8821. continue;
  8822. }
  8823. if (!sd->status.storage.items[i].unique_id && !itemdb_isstackable(nameid))
  8824. sd->status.storage.items[i].unique_id = pc_generate_unique_id(sd);
  8825. }
  8826. }
  8827. }
  8828. /*==========================================
  8829. * Update PVP rank for sd1 in cmp to sd2
  8830. *------------------------------------------*/
  8831. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8832. {
  8833. struct map_session_data *sd1,*sd2;
  8834. sd1=(struct map_session_data *)bl;
  8835. sd2=va_arg(ap,struct map_session_data *);
  8836. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8837. {// cannot register pvp rank for hidden GMs
  8838. return 0;
  8839. }
  8840. if( sd1->pvp_point > sd2->pvp_point )
  8841. sd2->pvp_rank++;
  8842. return 0;
  8843. }
  8844. /*==========================================
  8845. * Calculate new rank beetween all present players (map_foreachinarea)
  8846. * and display result
  8847. *------------------------------------------*/
  8848. int pc_calc_pvprank(struct map_session_data *sd)
  8849. {
  8850. int old = sd->pvp_rank;
  8851. struct map_data *m = &map[sd->bl.m];
  8852. sd->pvp_rank=1;
  8853. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8854. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8855. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8856. return sd->pvp_rank;
  8857. }
  8858. /*==========================================
  8859. * Calculate next sd ranking calculation from config
  8860. *------------------------------------------*/
  8861. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8862. {
  8863. struct map_session_data *sd;
  8864. sd=map_id2sd(id);
  8865. if(sd==NULL)
  8866. return 0;
  8867. sd->pvp_timer = INVALID_TIMER;
  8868. if( sd->sc.option&OPTION_INVISIBLE )
  8869. {// do not calculate the pvp rank for a hidden GM
  8870. return 0;
  8871. }
  8872. if( pc_calc_pvprank(sd) > 0 )
  8873. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8874. return 0;
  8875. }
  8876. /*==========================================
  8877. * Checking if sd is married
  8878. * Return:
  8879. * partner_id = yes
  8880. * 0 = no
  8881. *------------------------------------------*/
  8882. int pc_ismarried(struct map_session_data *sd)
  8883. {
  8884. if(sd == NULL)
  8885. return -1;
  8886. if(sd->status.partner_id > 0)
  8887. return sd->status.partner_id;
  8888. else
  8889. return 0;
  8890. }
  8891. /*==========================================
  8892. * Marry player sd to player dstsd
  8893. * Return:
  8894. * false = fail
  8895. * true = success
  8896. *------------------------------------------*/
  8897. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  8898. {
  8899. if(sd == NULL || dstsd == NULL ||
  8900. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  8901. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  8902. return false;
  8903. sd->status.partner_id = dstsd->status.char_id;
  8904. dstsd->status.partner_id = sd->status.char_id;
  8905. return true;
  8906. }
  8907. /*==========================================
  8908. * Divorce sd from its partner
  8909. * Return:
  8910. * false = fail
  8911. * true = success
  8912. *------------------------------------------*/
  8913. bool pc_divorce(struct map_session_data *sd)
  8914. {
  8915. struct map_session_data *p_sd;
  8916. int i;
  8917. if( sd == NULL || !pc_ismarried(sd) )
  8918. return false;
  8919. if( !sd->status.partner_id )
  8920. return false; // Char is not married
  8921. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8922. { // Lets char server do the divorce
  8923. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8924. return false; // No char server connected
  8925. return true;
  8926. }
  8927. // Both players online, lets do the divorce manually
  8928. sd->status.partner_id = 0;
  8929. p_sd->status.partner_id = 0;
  8930. for( i = 0; i < MAX_INVENTORY; i++ )
  8931. {
  8932. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8933. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8934. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8935. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8936. }
  8937. clif_divorced(sd, p_sd->status.name);
  8938. clif_divorced(p_sd, sd->status.name);
  8939. return true;
  8940. }
  8941. /**
  8942. * Get the partner map_session_data of a player
  8943. * @param sd : the husband|wife session
  8944. * @return partner session or NULL
  8945. */
  8946. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  8947. if (!sd || !pc_ismarried(sd))
  8948. return NULL;
  8949. return map_charid2sd(sd->status.partner_id);
  8950. }
  8951. /**
  8952. * Get the father map_session_data of a player
  8953. * @param sd : the baby session
  8954. * @return father session or NULL
  8955. */
  8956. struct map_session_data *pc_get_father (struct map_session_data *sd){
  8957. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  8958. return NULL;
  8959. return map_charid2sd(sd->status.father);
  8960. }
  8961. /**
  8962. * Get the mother map_session_data of a player
  8963. * @param sd : the baby session
  8964. * @return mother session or NULL
  8965. */
  8966. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  8967. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  8968. return NULL;
  8969. return map_charid2sd(sd->status.mother);
  8970. }
  8971. /*==========================================
  8972. * Get sd children charid. (Need to be married)
  8973. *------------------------------------------*/
  8974. struct map_session_data *pc_get_child (struct map_session_data *sd)
  8975. {
  8976. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  8977. // charid2sd returns NULL if not found
  8978. return NULL;
  8979. return map_charid2sd(sd->status.child);
  8980. }
  8981. /*==========================================
  8982. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  8983. *------------------------------------------*/
  8984. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  8985. {
  8986. int hp = 0, sp = 0;
  8987. if( pc_isdead(sd) )
  8988. return;
  8989. if (sd->hp_loss.value) {
  8990. sd->hp_loss.tick += diff_tick;
  8991. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  8992. hp += sd->hp_loss.value;
  8993. sd->hp_loss.tick -= sd->hp_loss.rate;
  8994. }
  8995. if(hp >= sd->battle_status.hp)
  8996. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  8997. }
  8998. if (sd->sp_loss.value) {
  8999. sd->sp_loss.tick += diff_tick;
  9000. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9001. sp += sd->sp_loss.value;
  9002. sd->sp_loss.tick -= sd->sp_loss.rate;
  9003. }
  9004. }
  9005. if (hp > 0 || sp > 0)
  9006. status_zap(&sd->bl, hp, sp);
  9007. }
  9008. //Character regen. Flag is used to know which types of regen can take place.
  9009. //&1: HP regen
  9010. //&2: SP regen
  9011. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9012. {
  9013. int hp = 0, sp = 0;
  9014. if (sd->hp_regen.value) {
  9015. sd->hp_regen.tick += diff_tick;
  9016. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9017. hp += sd->hp_regen.value;
  9018. sd->hp_regen.tick -= sd->hp_regen.rate;
  9019. }
  9020. }
  9021. if (sd->sp_regen.value) {
  9022. sd->sp_regen.tick += diff_tick;
  9023. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9024. sp += sd->sp_regen.value;
  9025. sd->sp_regen.tick -= sd->sp_regen.rate;
  9026. }
  9027. }
  9028. if (hp > 0 || sp > 0)
  9029. status_heal(&sd->bl, hp, sp, 0);
  9030. }
  9031. /*==========================================
  9032. * Memo player sd savepoint. (map,x,y)
  9033. *------------------------------------------*/
  9034. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9035. {
  9036. nullpo_retv(sd);
  9037. sd->status.save_point.map = mapindex;
  9038. sd->status.save_point.x = x;
  9039. sd->status.save_point.y = y;
  9040. }
  9041. /*==========================================
  9042. * Save 1 player data at autosave interval
  9043. *------------------------------------------*/
  9044. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9045. {
  9046. int interval;
  9047. struct s_mapiterator* iter;
  9048. struct map_session_data* sd;
  9049. static int last_save_id = 0, save_flag = 0;
  9050. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9051. save_flag = 0;
  9052. else
  9053. save_flag = 1; //Noone was saved, so save first found char.
  9054. iter = mapit_getallusers();
  9055. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9056. {
  9057. if(sd->bl.id == last_save_id && save_flag != 1) {
  9058. save_flag = 1;
  9059. continue;
  9060. }
  9061. if(save_flag != 1) //Not our turn to save yet.
  9062. continue;
  9063. //Save char.
  9064. last_save_id = sd->bl.id;
  9065. save_flag = 2;
  9066. #ifdef VIP_ENABLE
  9067. if(sd->vip.enabled) //check if we're still vip
  9068. chrif_req_login_operation(1, sd->status.name, 6, 0, 1, 0);
  9069. #endif
  9070. chrif_save(sd,0);
  9071. break;
  9072. }
  9073. mapit_free(iter);
  9074. interval = autosave_interval/(map_usercount()+1);
  9075. if(interval < minsave_interval)
  9076. interval = minsave_interval;
  9077. add_timer(gettick()+interval,pc_autosave,0,0);
  9078. return 0;
  9079. }
  9080. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9081. {
  9082. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9083. { //Night/day state does not match.
  9084. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  9085. sd->state.night = night_flag;
  9086. return 1;
  9087. }
  9088. return 0;
  9089. }
  9090. /*================================================
  9091. * timer to do the day [Yor]
  9092. * data: 0 = called by timer, 1 = gmcommand/script
  9093. *------------------------------------------------*/
  9094. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9095. {
  9096. char tmp_soutput[1024];
  9097. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9098. return 0;
  9099. if (!night_flag)
  9100. return 0; //Already day.
  9101. night_flag = 0; // 0=day, 1=night [Yor]
  9102. map_foreachpc(pc_daynight_timer_sub);
  9103. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9104. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9105. return 0;
  9106. }
  9107. /*================================================
  9108. * timer to do the night [Yor]
  9109. * data: 0 = called by timer, 1 = gmcommand/script
  9110. *------------------------------------------------*/
  9111. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9112. {
  9113. char tmp_soutput[1024];
  9114. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9115. return 0;
  9116. if (night_flag)
  9117. return 0; //Already nigth.
  9118. night_flag = 1; // 0=day, 1=night [Yor]
  9119. map_foreachpc(pc_daynight_timer_sub);
  9120. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9121. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9122. return 0;
  9123. }
  9124. /**
  9125. * Attempt to stand up a player
  9126. * @param sd
  9127. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9128. * @return True if success, Fals if failed
  9129. */
  9130. bool pc_setstand(struct map_session_data *sd, bool force){
  9131. nullpo_ret(sd);
  9132. // Cannot stand yet
  9133. // TODO: Move to SCS_NOSTAND [Cydh]
  9134. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9135. return false;
  9136. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9137. clif_status_load(&sd->bl,SI_SIT,0);
  9138. clif_standing(&sd->bl); //Inform area PC is standing
  9139. //Reset sitting tick.
  9140. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9141. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9142. return true;
  9143. }
  9144. /**
  9145. * Mechanic (MADO GEAR)
  9146. **/
  9147. void pc_overheat(struct map_session_data *sd, int val) {
  9148. int heat = val, skill,
  9149. limit[] = { 10, 20, 28, 46, 66 };
  9150. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9151. return; // already burning
  9152. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9153. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9154. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9155. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9156. }
  9157. heat = max(0,heat); // Avoid negative HEAT
  9158. if( heat >= limit[skill] )
  9159. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9160. else
  9161. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9162. return;
  9163. }
  9164. /**
  9165. * Check if player is autolooting given itemID.
  9166. */
  9167. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9168. {
  9169. uint8 i = 0;
  9170. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9171. return true;
  9172. if (!sd->state.autolooting)
  9173. return false;
  9174. if (sd->state.autolooting)
  9175. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9176. return (i != AUTOLOOTITEM_SIZE);
  9177. }
  9178. /**
  9179. * Checks if player can use @/#command
  9180. * @param sd Player map session data
  9181. * @param command Command name without @/# and params
  9182. * @param type is it atcommand or charcommand
  9183. */
  9184. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9185. {
  9186. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9187. }
  9188. /**
  9189. * Checks if commands used by a player should be logged
  9190. * according to their group setting.
  9191. * @param sd Player map session data
  9192. */
  9193. bool pc_should_log_commands(struct map_session_data *sd)
  9194. {
  9195. return pc_group_should_log_commands(pc_get_group_id(sd));
  9196. }
  9197. /**
  9198. * Spirit Charm expiration timer.
  9199. * @see TimerFunc
  9200. */
  9201. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9202. {
  9203. struct map_session_data *sd;
  9204. int i;
  9205. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9206. return 1;
  9207. if (sd->spiritcharm <= 0) {
  9208. 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);
  9209. sd->spiritcharm = 0;
  9210. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9211. return 0;
  9212. }
  9213. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9214. if (i == sd->spiritcharm) {
  9215. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9216. return 0;
  9217. }
  9218. sd->spiritcharm--;
  9219. if (i != sd->spiritcharm)
  9220. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9221. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9222. if (sd->spiritcharm <= 0)
  9223. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9224. clif_spiritcharm(sd);
  9225. return 0;
  9226. }
  9227. /**
  9228. * Adds a spirit charm.
  9229. * @param sd: Target character
  9230. * @param interval: Duration
  9231. * @param max: Maximum amount of charms to add
  9232. * @param type: Charm type (@see spirit_charm_types)
  9233. */
  9234. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9235. {
  9236. int tid, i;
  9237. nullpo_retv(sd);
  9238. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9239. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9240. if (max > MAX_SPIRITCHARM)
  9241. max = MAX_SPIRITCHARM;
  9242. if (sd->spiritcharm < 0)
  9243. sd->spiritcharm = 0;
  9244. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9245. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9246. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9247. sd->spiritcharm--;
  9248. if (sd->spiritcharm != 0)
  9249. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9250. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9251. }
  9252. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9253. 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);
  9254. if (i != sd->spiritcharm)
  9255. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9256. sd->spiritcharm_timer[i] = tid;
  9257. sd->spiritcharm++;
  9258. sd->spiritcharm_type = type;
  9259. clif_spiritcharm(sd);
  9260. }
  9261. /**
  9262. * Removes one or more spirit charms.
  9263. * @param sd: The target character
  9264. * @param count: Amount of charms to remove
  9265. * @param type: Type of charm to remove
  9266. */
  9267. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9268. {
  9269. int i;
  9270. nullpo_retv(sd);
  9271. if (sd->spiritcharm_type != type)
  9272. return;
  9273. if (sd->spiritcharm <= 0) {
  9274. sd->spiritcharm = 0;
  9275. return;
  9276. }
  9277. if (count <= 0)
  9278. return;
  9279. if (count > sd->spiritcharm)
  9280. count = sd->spiritcharm;
  9281. sd->spiritcharm -= count;
  9282. if (count > MAX_SPIRITCHARM)
  9283. count = MAX_SPIRITCHARM;
  9284. for (i = 0; i < count; i++) {
  9285. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9286. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9287. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9288. }
  9289. }
  9290. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9291. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9292. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9293. }
  9294. if (sd->spiritcharm <= 0)
  9295. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9296. clif_spiritcharm(sd);
  9297. }
  9298. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9299. /**
  9300. * Renewal EXP/Item Drop rate modifier based on level penalty
  9301. * @param level_diff: Monster and Player level difference
  9302. * @param mob_class: Monster class
  9303. * @param mode: Monster mode
  9304. * @param type: 1 - EXP, 2 - Item Drop
  9305. * @return Penalty rate
  9306. */
  9307. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9308. {
  9309. int rate = 100;
  9310. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9311. return rate;
  9312. if (level_diff < 0)
  9313. level_diff = MAX_LEVEL + (~level_diff + 1);
  9314. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9315. return rate;
  9316. return 100; // Penalty not found, return default
  9317. }
  9318. #endif
  9319. int pc_split_str(char *str,char **val,int num)
  9320. {
  9321. int i;
  9322. for (i=0; i<num && str; i++){
  9323. val[i] = str;
  9324. str = strchr(str,',');
  9325. if (str && i<num-1) //Do not remove a trailing comma.
  9326. *str++=0;
  9327. }
  9328. return i;
  9329. }
  9330. int pc_split_atoi(char* str, int* val, char sep, int max)
  9331. {
  9332. int i,j;
  9333. for (i=0; i<max; i++) {
  9334. if (!str) break;
  9335. val[i] = atoi(str);
  9336. str = strchr(str,sep);
  9337. if (str)
  9338. *str++=0;
  9339. }
  9340. //Zero up the remaining.
  9341. for(j=i; j < max; j++)
  9342. val[j] = 0;
  9343. return i;
  9344. }
  9345. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9346. {
  9347. static int warning=0;
  9348. int i,j;
  9349. for (i=0; i<max; i++) {
  9350. double f;
  9351. if (!str) break;
  9352. f = atof(str);
  9353. if (f < 0)
  9354. val[i] = 0;
  9355. else if (f > UINT_MAX) {
  9356. val[i] = UINT_MAX;
  9357. if (!warning) {
  9358. warning = 1;
  9359. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9360. }
  9361. } else
  9362. val[i] = (unsigned int)f;
  9363. str = strchr(str,sep);
  9364. if (str)
  9365. *str++=0;
  9366. }
  9367. //Zero up the remaining.
  9368. for(j=i; j < max; j++)
  9369. val[j] = 0;
  9370. return i;
  9371. }
  9372. /*==========================================
  9373. * sub DB reading.
  9374. * Function used to read skill_tree.txt
  9375. *------------------------------------------*/
  9376. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9377. {
  9378. unsigned char joblv = 0, skill_lv;
  9379. uint16 skill_id;
  9380. int idx, class_;
  9381. unsigned int i, offset = 3, skill_idx;
  9382. class_ = atoi(fields[0]);
  9383. skill_id = (uint16)atoi(fields[1]);
  9384. skill_lv = (unsigned char)atoi(fields[2]);
  9385. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  9386. {// job level requirement extra column
  9387. joblv = (unsigned char)atoi(fields[3]);
  9388. offset++;
  9389. }
  9390. if(!pcdb_checkid(class_))
  9391. {
  9392. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9393. return false;
  9394. }
  9395. idx = pc_class2idx(class_);
  9396. //This is to avoid adding two lines for the same skill. [Skotlex]
  9397. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  9398. if( skill_idx == MAX_SKILL_TREE )
  9399. {
  9400. 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_);
  9401. return false;
  9402. }
  9403. else if(skill_tree[idx][skill_idx].id)
  9404. {
  9405. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  9406. }
  9407. skill_tree[idx][skill_idx].id = skill_id;
  9408. skill_tree[idx][skill_idx].max = skill_lv;
  9409. skill_tree[idx][skill_idx].joblv = joblv;
  9410. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9411. {
  9412. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  9413. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  9414. }
  9415. return true;
  9416. }
  9417. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9418. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9419. {
  9420. int type, class_, diff;
  9421. type = atoi(fields[0]); //1=experience, 2=item drop
  9422. class_ = atoi(fields[1]);
  9423. diff = atoi(fields[2]);
  9424. if( type != 1 && type != 2 ){
  9425. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9426. return false;
  9427. }
  9428. if( !CHK_CLASS(class_) ){
  9429. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9430. return false;
  9431. }
  9432. diff = min(diff, MAX_LEVEL);
  9433. if( diff < 0 )
  9434. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9435. level_penalty[type][class_][diff] = atoi(fields[3]);
  9436. return true;
  9437. }
  9438. #endif
  9439. /** [Cydh]
  9440. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9441. * @param level Base level of player
  9442. * @param class_ Job ID @see enum e_job
  9443. * @return base_hp
  9444. */
  9445. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9446. double base_hp;
  9447. uint16 i, idx = pc_class2idx(class_);
  9448. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9449. #ifndef RENEWAL
  9450. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9451. #endif
  9452. for (i = 2; i <= level; i++)
  9453. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9454. if (class_ == JOB_SUMMONER)
  9455. base_hp += floor((base_hp / 2) + 0.5);
  9456. return (unsigned int)base_hp;
  9457. }
  9458. /** [Playtester]
  9459. * Calculates base sp of player.
  9460. * @param level Base level of player
  9461. * @param class_ Job ID @see enum e_job
  9462. * @return base_sp
  9463. */
  9464. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9465. double base_sp;
  9466. uint16 idx = pc_class2idx(class_);
  9467. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9468. switch (class_) {
  9469. case JOB_NINJA:
  9470. if (level >= 10)
  9471. base_sp -= 22;
  9472. else
  9473. base_sp = 11 + 3*level;
  9474. break;
  9475. case JOB_GUNSLINGER:
  9476. if (level > 10)
  9477. base_sp -= 18;
  9478. else
  9479. base_sp = 9 + 3*level;
  9480. break;
  9481. case JOB_SUMMONER:
  9482. base_sp -= floor(base_sp / 2);
  9483. break;
  9484. }
  9485. return (unsigned int)base_sp;
  9486. }
  9487. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9488. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9489. int idx, class_;
  9490. unsigned int i;
  9491. class_ = atoi(fields[0]);
  9492. if (!pcdb_checkid(class_)) {
  9493. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9494. return false;
  9495. }
  9496. idx = pc_class2idx(class_);
  9497. job_info[idx].max_weight_base = atoi(fields[1]);
  9498. job_info[idx].hp_factor = atoi(fields[2]);
  9499. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9500. job_info[idx].sp_factor = atoi(fields[4]);
  9501. #ifdef RENEWAL_ASPD
  9502. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9503. #else
  9504. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9505. #endif
  9506. {
  9507. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9508. }
  9509. return true;
  9510. }
  9511. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9512. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9513. {
  9514. int idx, class_, i;
  9515. class_ = atoi(fields[0]);
  9516. if(!pcdb_checkid(class_))
  9517. {
  9518. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9519. return false;
  9520. }
  9521. idx = pc_class2idx(class_);
  9522. for(i = 1; i < columns; i++)
  9523. {
  9524. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9525. }
  9526. return true;
  9527. }
  9528. //Reading job_exp.txt line
  9529. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9530. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9531. {
  9532. int idx, i, type;
  9533. int job_id,job_count,jobs[CLASS_COUNT];
  9534. unsigned int ui, maxlvl;
  9535. maxlvl = atoi(fields[0]);
  9536. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9537. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9538. return false;
  9539. }
  9540. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9541. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9542. return false;
  9543. }
  9544. type = atoi(fields[2]);
  9545. if(type < 0 || type > 1){
  9546. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9547. return false;
  9548. }
  9549. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9550. if (job_count < 1)
  9551. return false;
  9552. job_id = jobs[0];
  9553. if(!pcdb_checkid(job_id)){
  9554. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9555. return false;
  9556. }
  9557. idx = pc_class2idx(job_id);
  9558. job_info[idx].max_level[type] = maxlvl;
  9559. for(i=0; i<maxlvl; i++)
  9560. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9561. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9562. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9563. //should look like this:
  9564. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9565. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9566. job_info[idx].max_level[type]--;
  9567. if (job_info[idx].max_level[type] < maxlvl) {
  9568. 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]);
  9569. ShowInfo("Filling the missing values with the last exp entry.\n");
  9570. //Fill the requested values with the last entry.
  9571. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9572. for (; ui+2 < maxlvl; ui++)
  9573. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9574. job_info[idx].max_level[type] = maxlvl;
  9575. }
  9576. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9577. for (i = 1; i < job_count; i++) {
  9578. job_id = jobs[i];
  9579. if (!pcdb_checkid(job_id)) {
  9580. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9581. continue;
  9582. }
  9583. idx = pc_class2idx(job_id);
  9584. 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]));
  9585. job_info[idx].max_level[type] = maxlvl;
  9586. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9587. }
  9588. return true;
  9589. }
  9590. /**
  9591. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9592. * startlvl,endlvl,class,type,values...
  9593. */
  9594. #ifdef HP_SP_TABLES
  9595. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9596. {
  9597. int i, startlvl, endlvl;
  9598. int job_count,jobs[CLASS_COUNT];
  9599. short type;
  9600. startlvl = atoi(fields[0]);
  9601. if(startlvl<1){
  9602. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9603. return false;
  9604. }
  9605. endlvl = atoi(fields[1]);
  9606. if(endlvl<1 || endlvl<startlvl){
  9607. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9608. return false;
  9609. }
  9610. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9611. 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);
  9612. return false;
  9613. }
  9614. type = atoi(fields[3]);
  9615. if(type < 0 || type > 1){
  9616. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9617. return false;
  9618. }
  9619. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9620. if (job_count < 1)
  9621. return false;
  9622. for (i = 0; i < job_count; i++) {
  9623. int idx, job_id = jobs[i], use_endlvl;
  9624. if (!pcdb_checkid(job_id)) {
  9625. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9626. return false;
  9627. }
  9628. idx = pc_class2idx(job_id);
  9629. if (startlvl > job_info[idx].max_level[0]) {
  9630. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9631. return false;
  9632. }
  9633. //Just read until available max level for this job, don't use MAX_LEVEL!
  9634. use_endlvl = endlvl;
  9635. if (use_endlvl > job_info[idx].max_level[0])
  9636. use_endlvl = job_info[idx].max_level[0];
  9637. if(type == 0) { //hp type
  9638. uint16 j;
  9639. for(j = 0; j < use_endlvl; j++) {
  9640. if (atoi(fields[j+4])) {
  9641. uint16 lvl_idx = startlvl-1+j;
  9642. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9643. //Tells if this HP is lower than previous level (but not for 99->100)
  9644. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9645. 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",
  9646. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9647. }
  9648. }
  9649. }
  9650. else { //sp type
  9651. uint16 j;
  9652. for(j = 0; j < use_endlvl; j++) {
  9653. if (atoi(fields[j+4])) {
  9654. uint16 lvl_idx = startlvl-1+j;
  9655. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9656. //Tells if this SP is lower than previous level (but not for 99->100)
  9657. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9658. 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",
  9659. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9660. }
  9661. }
  9662. }
  9663. }
  9664. return true;
  9665. }
  9666. #endif
  9667. /** [Cydh]
  9668. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9669. */
  9670. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9671. {
  9672. int idx, class_;
  9673. uint16 str, agi, vit, int_, dex, luk;
  9674. script_get_constant(trim(fields[0]),&class_);
  9675. if ((idx = pc_class2idx(class_)) < 0) {
  9676. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9677. return false;
  9678. }
  9679. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9680. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9681. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9682. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9683. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9684. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9685. job_info[idx].max_param.str = str;
  9686. job_info[idx].max_param.agi = agi;
  9687. job_info[idx].max_param.vit = vit;
  9688. job_info[idx].max_param.int_ = int_;
  9689. job_info[idx].max_param.dex = dex;
  9690. job_info[idx].max_param.luk = luk;
  9691. return true;
  9692. }
  9693. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9694. int i=1;
  9695. char line[24000]; //FIXME this seem too big
  9696. FILE *fp;
  9697. sprintf(line, "%s/statpoint.txt", basedir);
  9698. fp=fopen(line,"r");
  9699. if(fp == NULL){
  9700. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9701. return max(last_s,i);
  9702. } else {
  9703. int entries=0;
  9704. while(fgets(line, sizeof(line), fp))
  9705. {
  9706. int stat;
  9707. trim(line);
  9708. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9709. continue;
  9710. if ((stat=strtoul(line,NULL,10))<0)
  9711. stat=0;
  9712. if (i > MAX_LEVEL)
  9713. break;
  9714. statp[i]=stat;
  9715. i++;
  9716. entries++;
  9717. }
  9718. fclose(fp);
  9719. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9720. }
  9721. return max(last_s,i);
  9722. }
  9723. /*==========================================
  9724. * pc DB reading.
  9725. * job_exp.txt - required experience values
  9726. * skill_tree.txt - skill tree for every class
  9727. * attr_fix.txt - elemental adjustment table
  9728. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9729. * job_db2.txt - job,stats bonuses/lvl
  9730. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9731. *------------------------------------------*/
  9732. void pc_readdb(void) {
  9733. int i, k, s = 1;
  9734. const char* dbsubpath[] = {
  9735. "",
  9736. "/"DBIMPORT,
  9737. //add other path here
  9738. };
  9739. //reset
  9740. memset(job_info,0,sizeof(job_info)); // job_info table
  9741. // Reset and read skilltree
  9742. memset(skill_tree,0,sizeof(skill_tree));
  9743. 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);
  9744. 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);
  9745. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9746. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9747. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9748. for( k=1; k < 3; k++ ){ // fill in the blanks
  9749. int j;
  9750. for( j = 0; j < CLASS_ALL; j++ ){
  9751. int tmp = 0;
  9752. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9753. if( i == MAX_LEVEL+1 )
  9754. tmp = level_penalty[k][j][0];// reset
  9755. if( level_penalty[k][j][i] > 0 )
  9756. tmp = level_penalty[k][j][i];
  9757. else
  9758. level_penalty[k][j][i] = tmp;
  9759. }
  9760. }
  9761. }
  9762. #endif
  9763. // reset then read statspoint
  9764. memset(statp,0,sizeof(statp));
  9765. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9766. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  9767. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  9768. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9769. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9770. if(i==0) {
  9771. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9772. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9773. }
  9774. else {
  9775. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9776. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9777. }
  9778. s = pc_read_statsdb(dbsubpath2,s,i);
  9779. if (i == 0)
  9780. #ifdef RENEWAL_ASPD
  9781. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9782. #else
  9783. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9784. #endif
  9785. else
  9786. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9787. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  9788. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  9789. #ifdef HP_SP_TABLES
  9790. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  9791. #endif
  9792. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  9793. aFree(dbsubpath1);
  9794. aFree(dbsubpath2);
  9795. }
  9796. // generate the remaining parts of the db if necessary
  9797. k = battle_config.use_statpoint_table; //save setting
  9798. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  9799. statp[0] = 45; // seed value
  9800. for (; s <= MAX_LEVEL; s++)
  9801. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  9802. battle_config.use_statpoint_table = k; //restore setting
  9803. //Checking if all class have their data
  9804. for (i = 0; i < JOB_MAX; i++) {
  9805. int idx;
  9806. uint16 j;
  9807. if (!pcdb_checkid(i))
  9808. continue;
  9809. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  9810. continue; //Classes that do not need exp tables.
  9811. idx = pc_class2idx(i);
  9812. if (!job_info[idx].max_level[0])
  9813. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  9814. if (!job_info[idx].max_level[1])
  9815. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  9816. //Init and checking the empty value of Base HP/SP [Cydh]
  9817. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  9818. if (job_info[idx].base_hp[j] == 0)
  9819. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  9820. if (job_info[idx].base_sp[j] == 0)
  9821. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  9822. }
  9823. }
  9824. }
  9825. // Read MOTD on startup. [Valaris]
  9826. int pc_read_motd(void)
  9827. {
  9828. FILE* fp;
  9829. // clear old MOTD
  9830. memset(motd_text, 0, sizeof(motd_text));
  9831. // read current MOTD
  9832. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  9833. {
  9834. unsigned int entries = 0;
  9835. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  9836. {
  9837. char* buf = motd_text[entries];
  9838. unsigned int lines = 0;
  9839. size_t len;
  9840. lines++;
  9841. if( buf[0] == '/' && buf[1] == '/' )
  9842. continue;
  9843. len = strlen(buf);
  9844. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  9845. len--;
  9846. if( len ) {
  9847. char * ptr;
  9848. buf[len] = 0;
  9849. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  9850. 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);
  9851. }
  9852. else {// empty line
  9853. buf[0] = ' ';
  9854. buf[1] = 0;
  9855. }
  9856. entries++;
  9857. }
  9858. fclose(fp);
  9859. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  9860. }
  9861. else
  9862. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  9863. return 0;
  9864. }
  9865. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  9866. int i,cursor = 0;
  9867. struct item_cd* cd = NULL;
  9868. if( load ) {
  9869. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  9870. // no item cooldown is associated with this character
  9871. return;
  9872. }
  9873. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9874. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  9875. sd->item_delay[cursor].tick = cd->tick[i];
  9876. sd->item_delay[cursor].nameid = cd->nameid[i];
  9877. cursor++;
  9878. }
  9879. }
  9880. idb_remove(itemcd_db,sd->status.char_id);
  9881. } else {
  9882. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  9883. // create a new skill cooldown object for map storage
  9884. CREATE( cd, struct item_cd, 1 );
  9885. idb_put( itemcd_db, sd->status.char_id, cd );
  9886. }
  9887. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9888. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  9889. cd->tick[cursor] = sd->item_delay[i].tick;
  9890. cd->nameid[cursor] = sd->item_delay[i].nameid;
  9891. cursor++;
  9892. }
  9893. }
  9894. }
  9895. return;
  9896. }
  9897. /**
  9898. * Add item delay to player's item delay data
  9899. * @param sd Player
  9900. * @param id Item data
  9901. * @param tick Current tick
  9902. * @param n Item index in inventory
  9903. * @return 0: No delay, can consume item.
  9904. * 1: Has delay, cancel consumption.
  9905. **/
  9906. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  9907. int i;
  9908. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  9909. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  9910. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  9911. if( i < MAX_ITEMDELAYS ) {
  9912. if( sd->item_delay[i].nameid ) {// found
  9913. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  9914. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  9915. char e_msg[CHAT_SIZE_MAX];
  9916. if( e_tick > 99 )
  9917. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  9918. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  9919. else
  9920. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  9921. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  9922. clif_colormes(sd->fd,color_table[COLOR_YELLOW],e_msg);
  9923. return 1; // Delay has not expired yet
  9924. }
  9925. } else {// not yet used item (all slots are initially empty)
  9926. sd->item_delay[i].nameid = id->nameid;
  9927. }
  9928. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  9929. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  9930. } else {// should not happen
  9931. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  9932. }
  9933. //clean up used delays so we can give room for more
  9934. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9935. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  9936. sd->item_delay[i].tick = 0;
  9937. sd->item_delay[i].nameid = 0;
  9938. }
  9939. }
  9940. return 0;
  9941. }
  9942. /**
  9943. * Check if player has delay to reuse item
  9944. * @param sd Player
  9945. * @param id Item data
  9946. * @param tick Current tick
  9947. * @param n Item index in inventory
  9948. * @return 0: No delay, can consume item.
  9949. * 1: Has delay, cancel consumption.
  9950. **/
  9951. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  9952. struct status_change *sc = NULL;
  9953. nullpo_retr(0, sd);
  9954. nullpo_retr(0, id);
  9955. // Do normal delay assignment
  9956. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  9957. return pc_itemcd_add(sd, id, tick, n);
  9958. // Send reply of delay remains
  9959. if (sc->data[id->delay_sc]) {
  9960. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  9961. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  9962. return 1;
  9963. }
  9964. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  9965. return 0;
  9966. }
  9967. /**
  9968. * Clear the dmglog data from player
  9969. * @param sd
  9970. * @param md
  9971. **/
  9972. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  9973. {
  9974. uint8 i;
  9975. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  9976. if (i < DAMAGELOG_SIZE) {
  9977. md->dmglog[i].id = 0;
  9978. md->dmglog[i].dmg = 0;
  9979. md->dmglog[i].flag = 0;
  9980. }
  9981. }
  9982. /**
  9983. * Add log to player's dmglog
  9984. * @param sd
  9985. * @param id Monster's GID
  9986. **/
  9987. void pc_damage_log_add(struct map_session_data *sd, int id)
  9988. {
  9989. uint8 i = 0;
  9990. if (!sd || !id)
  9991. return;
  9992. //Only store new data, don't need to renew the old one with same id
  9993. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  9994. if (i < DAMAGELOG_SIZE_PC)
  9995. return;
  9996. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  9997. if (sd->dmglog[i] == 0) {
  9998. sd->dmglog[i] = id;
  9999. return;
  10000. }
  10001. }
  10002. }
  10003. /**
  10004. * Clear dmglog data from player
  10005. * @param sd
  10006. * @param id Monster's id
  10007. **/
  10008. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10009. {
  10010. uint8 i;
  10011. struct mob_data *md = NULL;
  10012. if (!sd)
  10013. return;
  10014. if (!id) {
  10015. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10016. if( !sd->dmglog[i] ) //skip the empty value
  10017. continue;
  10018. if ((md = map_id2md(sd->dmglog[i])))
  10019. pc_clear_log_damage_sub(sd->status.char_id,md);
  10020. sd->dmglog[i] = 0;
  10021. }
  10022. }
  10023. else {
  10024. if ((md = map_id2md(id)))
  10025. pc_clear_log_damage_sub(sd->status.char_id,md);
  10026. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10027. if (i < DAMAGELOG_SIZE_PC)
  10028. sd->dmglog[i] = 0;
  10029. }
  10030. }
  10031. /**
  10032. * Status change data arrived from char-server
  10033. * @param sd: Player data
  10034. */
  10035. void pc_scdata_received(struct map_session_data *sd) {
  10036. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10037. }
  10038. /**
  10039. * Check player account expiration time and rental item expirations
  10040. * @param sd: Player data
  10041. */
  10042. void pc_check_expiration(struct map_session_data *sd) {
  10043. #ifndef ENABLE_SC_SAVING
  10044. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10045. #endif
  10046. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10047. time_t exp_time = sd->expiration_time;
  10048. char tmpstr[1024];
  10049. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10050. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  10051. pc_expire_check(sd);
  10052. }
  10053. }
  10054. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10055. struct map_session_data *sd = map_id2sd(id);
  10056. if( !sd ) return 0;
  10057. sd->expiration_tid = INVALID_TIMER;
  10058. if( sd->fd )
  10059. clif_authfail_fd(sd->fd,10);
  10060. map_quit(sd);
  10061. return 0;
  10062. }
  10063. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10064. struct map_session_data *sd = map_id2sd(id);
  10065. if (!sd)
  10066. return 0;
  10067. sd->autotrade_tid = INVALID_TIMER;
  10068. if (sd->state.autotrade&2)
  10069. vending_reopen(sd);
  10070. if (sd->state.autotrade&4)
  10071. buyingstore_reopen(sd);
  10072. if (!sd->vender_id && !sd->buyer_id) {
  10073. sd->state.autotrade = 0;
  10074. map_quit(sd);
  10075. }
  10076. return 0;
  10077. }
  10078. /* this timer exists only when a character with a expire timer > 24h is online */
  10079. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10080. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10081. struct s_mapiterator* iter;
  10082. struct map_session_data* sd;
  10083. iter = mapit_getallusers();
  10084. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10085. if( sd->expiration_time )
  10086. pc_expire_check(sd);
  10087. mapit_free(iter);
  10088. return 0;
  10089. }
  10090. void pc_expire_check(struct map_session_data *sd) {
  10091. /* ongoing timer */
  10092. if( sd->expiration_tid != INVALID_TIMER )
  10093. return;
  10094. /* not within the next 24h, enable the global check */
  10095. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10096. /* global check not running, enable */
  10097. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10098. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10099. return;
  10100. }
  10101. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10102. }
  10103. /**
  10104. * Deposit some money to bank
  10105. * @param sd
  10106. * @param money Amount of money to deposit
  10107. **/
  10108. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10109. unsigned int limit_check = money + sd->bank_vault;
  10110. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10111. return BDA_OVERFLOW;
  10112. } else if ( money > sd->status.zeny ) {
  10113. return BDA_NO_MONEY;
  10114. }
  10115. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10116. return BDA_NO_MONEY;
  10117. sd->bank_vault += money;
  10118. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10119. if( save_settings&CHARSAVE_BANK )
  10120. chrif_save(sd,0);
  10121. return BDA_SUCCESS;
  10122. }
  10123. /**
  10124. * Withdraw money from bank
  10125. * @param sd
  10126. * @param money Amount of money that will be withdrawn
  10127. **/
  10128. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10129. unsigned int limit_check = money + sd->status.zeny;
  10130. if( money <= 0 ) {
  10131. return BWA_UNKNOWN_ERROR;
  10132. } else if ( money > sd->bank_vault ) {
  10133. return BWA_NO_MONEY;
  10134. } else if ( limit_check > MAX_ZENY ) {
  10135. /* no official response for this scenario exists. */
  10136. clif_colormes(sd->fd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  10137. return BWA_UNKNOWN_ERROR;
  10138. }
  10139. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10140. return BWA_NO_MONEY;
  10141. sd->bank_vault -= money;
  10142. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10143. if( save_settings&CHARSAVE_BANK )
  10144. chrif_save(sd,0);
  10145. return BWA_SUCCESS;
  10146. }
  10147. /**
  10148. * Clear Crimson Marker data from caster
  10149. * @param sd: Player
  10150. **/
  10151. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10152. uint8 i;
  10153. if (!sd)
  10154. return;
  10155. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10156. struct block_list *bl = NULL;
  10157. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10158. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10159. sd->c_marker[i] = 0;
  10160. }
  10161. }
  10162. /**
  10163. * Show version to player
  10164. * @param sd: Player
  10165. **/
  10166. void pc_show_version(struct map_session_data *sd) {
  10167. const char* svn = get_svn_revision();
  10168. char buf[CHAT_SIZE_MAX];
  10169. if( svn[0] != UNKNOWN_VERSION )
  10170. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10171. else {
  10172. const char* git = get_git_hash();
  10173. if( git[0] != UNKNOWN_VERSION )
  10174. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10175. else
  10176. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10177. }
  10178. clif_displaymessage(sd->fd,buf);
  10179. }
  10180. /**
  10181. * Run bonus_script on player
  10182. * @param sd
  10183. * @author [Cydh]
  10184. **/
  10185. void pc_bonus_script(struct map_session_data *sd) {
  10186. int now = gettick();
  10187. struct linkdb_node *node = NULL, *next = NULL;
  10188. if (!sd || !(node = sd->bonus_script.head))
  10189. return;
  10190. while (node) {
  10191. struct s_bonus_script_entry *entry = NULL;
  10192. next = node->next;
  10193. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10194. // Only start timer for new bonus_script
  10195. if (entry->tid == INVALID_TIMER) {
  10196. if (entry->icon != SI_BLANK) // Gives status icon if exist
  10197. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10198. entry->tick += now;
  10199. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10200. }
  10201. if (entry->script)
  10202. run_script(entry->script, 0, sd->bl.id, 0);
  10203. else
  10204. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10205. }
  10206. node = next;
  10207. }
  10208. }
  10209. /**
  10210. * Add bonus_script to player
  10211. * @param sd Player
  10212. * @param script_str Script string
  10213. * @param dur Duration in ms
  10214. * @param icon SI
  10215. * @param flag Flags @see enum e_bonus_script_flags
  10216. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10217. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10218. * @author [Cydh]
  10219. **/
  10220. 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) {
  10221. struct script_code *script = NULL;
  10222. struct linkdb_node *node = NULL;
  10223. struct s_bonus_script_entry *entry = NULL;
  10224. if (!sd)
  10225. return NULL;
  10226. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10227. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10228. return NULL;
  10229. }
  10230. // Duplication checks
  10231. if ((node = sd->bonus_script.head)) {
  10232. while (node) {
  10233. entry = (struct s_bonus_script_entry *)node->data;
  10234. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10235. int newdur = gettick() + dur;
  10236. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10237. settick_timer(entry->tid, newdur);
  10238. script_free_code(script);
  10239. return NULL;
  10240. }
  10241. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10242. break;
  10243. else { // No duplicate bonus
  10244. script_free_code(script);
  10245. return NULL;
  10246. }
  10247. }
  10248. node = node->next;
  10249. }
  10250. }
  10251. CREATE(entry, struct s_bonus_script_entry, 1);
  10252. entry->script_buf = StringBuf_Malloc();
  10253. StringBuf_AppendStr(entry->script_buf, script_str);
  10254. entry->tid = INVALID_TIMER;
  10255. entry->flag = flag;
  10256. entry->icon = icon;
  10257. entry->tick = dur; // Use duration first, on run change to expire time
  10258. entry->type = type;
  10259. entry->script = script;
  10260. sd->bonus_script.count++;
  10261. return entry;
  10262. }
  10263. /**
  10264. * Remove bonus_script data from player
  10265. * @param sd: Target player
  10266. * @param list: Bonus script entry from player
  10267. * @author [Cydh]
  10268. **/
  10269. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10270. if (entry->tid != INVALID_TIMER)
  10271. delete_timer(entry->tid, pc_bonus_script_timer);
  10272. if (entry->script)
  10273. script_free_code(entry->script);
  10274. if (entry->script_buf)
  10275. StringBuf_Free(entry->script_buf);
  10276. if (sd) {
  10277. if (entry->icon != SI_BLANK)
  10278. clif_status_load(&sd->bl, entry->icon, 0);
  10279. if (sd->bonus_script.count > 0)
  10280. sd->bonus_script.count--;
  10281. }
  10282. aFree(entry);
  10283. }
  10284. /**
  10285. * Do final process if no entry left
  10286. * @param sd
  10287. **/
  10288. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10289. if (sd->bonus_script.count == 0) {
  10290. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10291. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10292. linkdb_final(&sd->bonus_script.head);
  10293. }
  10294. }
  10295. /**
  10296. * Timer for bonus_script
  10297. * @param tid
  10298. * @param tick
  10299. * @param id
  10300. * @param data
  10301. * @author [Cydh]
  10302. **/
  10303. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10304. struct map_session_data *sd;
  10305. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10306. sd = map_id2sd(id);
  10307. if (!sd) {
  10308. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10309. return 0;
  10310. }
  10311. if (tid == INVALID_TIMER)
  10312. return 0;
  10313. if (!sd->bonus_script.head || entry == NULL) {
  10314. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10315. return 0;
  10316. }
  10317. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10318. pc_bonus_script_free_entry(sd, entry);
  10319. pc_bonus_script_check_final(sd);
  10320. status_calc_pc(sd,SCO_NONE);
  10321. return 0;
  10322. }
  10323. /**
  10324. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10325. * @param sd: Target player
  10326. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10327. * @author [Cydh]
  10328. **/
  10329. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10330. struct linkdb_node *node = NULL;
  10331. uint16 count = 0;
  10332. if (!sd || !(node = sd->bonus_script.head))
  10333. return;
  10334. while (node) {
  10335. struct linkdb_node *next = node->next;
  10336. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10337. if (entry && (
  10338. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10339. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10340. (flag&entry->flag) || // Matched flag
  10341. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10342. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10343. )
  10344. )
  10345. {
  10346. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10347. pc_bonus_script_free_entry(sd, entry);
  10348. count++;
  10349. }
  10350. node = next;
  10351. }
  10352. pc_bonus_script_check_final(sd);
  10353. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10354. status_calc_pc(sd,SCO_NONE);
  10355. }
  10356. /** [Cydh]
  10357. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10358. * @param sd: Target player
  10359. */
  10360. void pc_cell_basilica(struct map_session_data *sd) {
  10361. nullpo_retv(sd);
  10362. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10363. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10364. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10365. }
  10366. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10367. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10368. }
  10369. /** [Cydh]
  10370. * Get maximum specified parameter for specified class
  10371. * @param class_: sd->class
  10372. * @param sex: sd->status.sex
  10373. * @param flag: parameter will be checked
  10374. * @return max_param
  10375. */
  10376. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10377. int idx = -1, class_ = sd->class_;
  10378. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10379. short max_param = 0;
  10380. switch (param) {
  10381. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10382. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10383. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10384. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10385. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10386. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10387. }
  10388. if (max_param > 0)
  10389. return max_param;
  10390. }
  10391. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10392. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10393. ((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)) :
  10394. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10395. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10396. }
  10397. /**
  10398. * Get max ASPD for player based on Class
  10399. * @param sd Player
  10400. * @return ASPD
  10401. */
  10402. short pc_maxaspd(struct map_session_data *sd) {
  10403. nullpo_ret(sd);
  10404. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10405. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10406. battle_config.max_aspd ));
  10407. }
  10408. /**
  10409. * Calculates total item-group related bonuses for the given item
  10410. * @param sd Player
  10411. * @param nameid Item ID
  10412. * @return Heal rate
  10413. **/
  10414. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10415. short bonus = 0;
  10416. uint8 i;
  10417. if (!sd->itemgrouphealrate_count)
  10418. return bonus;
  10419. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10420. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10421. struct s_item_group_db *group = NULL;
  10422. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10423. continue;
  10424. for (j = 0; j < group->random[0].data_qty; j++) {
  10425. if (group->random[0].data[j].nameid == nameid) {
  10426. bonus += sd->itemgrouphealrate[i]->rate;
  10427. break;
  10428. }
  10429. }
  10430. }
  10431. return bonus;
  10432. }
  10433. /**
  10434. * Calculates total item-group related bonuses for the given item group
  10435. * @param sd Player
  10436. * @param group_id Item Group ID
  10437. * @return Heal rate
  10438. **/
  10439. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10440. short bonus = 0;
  10441. uint8 i;
  10442. if (!sd->itemgrouphealrate_count)
  10443. return bonus;
  10444. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10445. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10446. return sd->itemgrouphealrate[i]->rate;
  10447. }
  10448. return bonus;
  10449. }
  10450. /**
  10451. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10452. * @param eqi Item EQI of enum equip_index
  10453. * @param *equip_index Player's equip_index[]
  10454. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10455. * @return True if item in same inventory index, False if doesn't
  10456. */
  10457. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10458. if (index < 0 || index >= MAX_INVENTORY)
  10459. return true;
  10460. // Dual weapon checks
  10461. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10462. return true;
  10463. // Headgear with Mid & Low location
  10464. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10465. return true;
  10466. // Headgear with Top & Mid or Low location
  10467. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10468. return true;
  10469. // Headgear with Mid & Low location
  10470. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10471. return true;
  10472. // Headgear with Top & Mid or Low location
  10473. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10474. return true;
  10475. return false;
  10476. }
  10477. /**
  10478. * Generate Unique item ID for player
  10479. * @param sd : Player
  10480. * @return A generated Unique item ID
  10481. */
  10482. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10483. nullpo_ret(sd);
  10484. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10485. }
  10486. /**
  10487. * Validating skill from player after logged on
  10488. * @param sd
  10489. **/
  10490. void pc_validate_skill(struct map_session_data *sd) {
  10491. if (sd) {
  10492. uint16 i = 0, count = 0;
  10493. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10494. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10495. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10496. for (i = 0; i < MAX_SKILL; i++) {
  10497. uint16 idx = 0;
  10498. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10499. continue;
  10500. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10501. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10502. count++;
  10503. }
  10504. else
  10505. 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);
  10506. }
  10507. }
  10508. }
  10509. /**
  10510. * Toggle to remember if the questinfo is displayed yet or not.
  10511. * @param qi_display Display flag
  10512. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10513. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10514. **/
  10515. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10516. if (show) {
  10517. // Check if need to be displayed
  10518. if ((*qi_display) != 1) {
  10519. (*qi_display) = 1;
  10520. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10521. }
  10522. }
  10523. else {
  10524. // Check if need to be hide
  10525. if ((*qi_display) != 0) {
  10526. (*qi_display) = 0;
  10527. #if PACKETVER >= 20120410
  10528. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10529. #else
  10530. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10531. #endif
  10532. }
  10533. }
  10534. }
  10535. /**
  10536. * Show available NPC Quest / Event Icon Check [Kisuka]
  10537. * @param sd Player
  10538. **/
  10539. void pc_show_questinfo(struct map_session_data *sd) {
  10540. #if PACKETVER >= 20090218
  10541. struct questinfo *qi = NULL;
  10542. unsigned short i;
  10543. uint8 j;
  10544. int8 mystate = 0;
  10545. bool failed = false;
  10546. nullpo_retv(sd);
  10547. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10548. return;
  10549. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10550. return;
  10551. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10552. qi = &map[sd->bl.m].qi_data[i];
  10553. if (!qi)
  10554. continue;
  10555. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10556. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10557. continue;
  10558. }
  10559. // Level range checks
  10560. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10561. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10562. continue;
  10563. }
  10564. // Quest requirements
  10565. if (qi->req_count) {
  10566. failed = false;
  10567. for (j = 0; j < qi->req_count; j++) {
  10568. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10569. mystate = mystate + (mystate < 1);
  10570. if (mystate != qi->req[j].state) {
  10571. failed = true;
  10572. break;
  10573. }
  10574. }
  10575. if (failed) {
  10576. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10577. continue;
  10578. }
  10579. }
  10580. // Job requirements
  10581. if (qi->jobid_count) {
  10582. failed = true;
  10583. for (j = 0; j < qi->jobid_count; j++) {
  10584. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10585. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10586. failed = false;
  10587. break;
  10588. }
  10589. }
  10590. if (!failed)
  10591. continue;
  10592. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10593. }
  10594. else {
  10595. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10596. }
  10597. }
  10598. #endif
  10599. }
  10600. /**
  10601. * Reinit the questinfo for player when changing map
  10602. * @param sd Player
  10603. **/
  10604. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10605. #if PACKETVER >= 20090218
  10606. nullpo_retv(sd);
  10607. if (sd->qi_display) {
  10608. aFree(sd->qi_display);
  10609. sd->qi_display = NULL;
  10610. }
  10611. sd->qi_count = 0;
  10612. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10613. return;
  10614. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10615. return;
  10616. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10617. #endif
  10618. }
  10619. /*==========================================
  10620. * pc Init/Terminate
  10621. *------------------------------------------*/
  10622. void do_final_pc(void) {
  10623. db_destroy(itemcd_db);
  10624. do_final_pc_groups();
  10625. ers_destroy(pc_sc_display_ers);
  10626. ers_destroy(pc_itemgrouphealrate_ers);
  10627. ers_destroy(num_reg_ers);
  10628. ers_destroy(str_reg_ers);
  10629. }
  10630. void do_init_pc(void) {
  10631. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10632. pc_readdb();
  10633. pc_read_motd(); // Read MOTD [Valaris]
  10634. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10635. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10636. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10637. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10638. add_timer_func_list(pc_autosave, "pc_autosave");
  10639. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10640. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10641. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10642. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10643. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10644. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10645. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10646. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10647. // 0=day, 1=night [Yor]
  10648. night_flag = battle_config.night_at_start ? 1 : 0;
  10649. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10650. int day_duration = battle_config.day_duration;
  10651. int night_duration = battle_config.night_duration;
  10652. // add night/day timer [Yor]
  10653. add_timer_func_list(map_day_timer, "map_day_timer");
  10654. add_timer_func_list(map_night_timer, "map_night_timer");
  10655. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10656. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10657. }
  10658. do_init_pc_groups();
  10659. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  10660. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10661. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10662. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10663. ers_chunk_size(pc_sc_display_ers, 150);
  10664. ers_chunk_size(num_reg_ers, 300);
  10665. ers_chunk_size(str_reg_ers, 50);
  10666. }