pc.cpp 400 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/ers.hpp" // ers_destroy
  11. #include "../common/malloc.hpp"
  12. #include "../common/mmo.hpp" //NAME_LENGTH
  13. #include "../common/nullpo.hpp"
  14. #include "../common/random.hpp"
  15. #include "../common/showmsg.hpp"
  16. #include "../common/socket.hpp" // session[]
  17. #include "../common/strlib.hpp" // safestrncpy()
  18. #include "../common/timer.hpp"
  19. #include "../common/utilities.hpp"
  20. #include "../common/utils.hpp"
  21. #include "achievement.hpp"
  22. #include "atcommand.hpp" // get_atcommand_level()
  23. #include "battle.hpp" // battle_config
  24. #include "battleground.hpp"
  25. #include "buyingstore.hpp" // struct s_buyingstore
  26. #include "channel.hpp"
  27. #include "chat.hpp"
  28. #include "chrif.hpp"
  29. #include "clan.hpp"
  30. #include "clif.hpp"
  31. #include "date.hpp" // is_day_of_*()
  32. #include "duel.hpp"
  33. #include "elemental.hpp"
  34. #include "guild.hpp"
  35. #include "homunculus.hpp"
  36. #include "instance.hpp"
  37. #include "intif.hpp"
  38. #include "itemdb.hpp" // MAX_ITEMGROUP
  39. #include "log.hpp"
  40. #include "map.hpp"
  41. #include "mercenary.hpp"
  42. #include "mob.hpp"
  43. #include "npc.hpp"
  44. #include "party.hpp" // party_search()
  45. #include "pc_groups.hpp"
  46. #include "pet.hpp" // pet_unlocktarget()
  47. #include "quest.hpp"
  48. #include "script.hpp" // struct script_reg, struct script_regstr
  49. #include "searchstore.hpp" // struct s_search_store_info
  50. #include "status.hpp" // OPTION_*, struct weapon_atk
  51. #include "storage.hpp"
  52. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  53. #include "vending.hpp" // struct s_vending
  54. using namespace rathena;
  55. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  56. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  57. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  58. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  59. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  60. static unsigned int statp[MAX_LEVEL+1];
  61. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  62. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  63. #endif
  64. // h-files are for declarations, not for implementations... [Shinomori]
  65. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  66. // timer for night.day implementation
  67. int day_timer_tid = INVALID_TIMER;
  68. int night_timer_tid = INVALID_TIMER;
  69. struct eri *pc_sc_display_ers = NULL;
  70. struct eri *pc_itemgrouphealrate_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. struct s_attendance_reward{
  79. uint16 item_id;
  80. uint16 amount;
  81. };
  82. struct s_attendance_period{
  83. uint32 start;
  84. uint32 end;
  85. std::map<int,struct s_attendance_reward> rewards;
  86. };
  87. std::vector<struct s_attendance_period> attendance_periods;
  88. #define MOTD_LINE_SIZE 128
  89. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  90. bool reg_load;
  91. /**
  92. * Translation table from athena equip index to aegis bitmask
  93. */
  94. unsigned int equip_bitmask[EQI_MAX] = {
  95. EQP_ACC_L, // EQI_ACC_L
  96. EQP_ACC_R, // EQI_ACC_R
  97. EQP_SHOES, // EQI_SHOES
  98. EQP_GARMENT, // EQI_GARMENT
  99. EQP_HEAD_LOW, // EQI_HEAD_LOW
  100. EQP_HEAD_MID, // EQI_HEAD_MID
  101. EQP_HEAD_TOP, // EQI_HEAD_TOP
  102. EQP_ARMOR, // EQI_ARMOR
  103. EQP_HAND_L, // EQI_HAND_L
  104. EQP_HAND_R, // EQI_HAND_R
  105. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  106. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  107. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  108. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  109. EQP_AMMO, // EQI_AMMO
  110. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  111. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  112. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  113. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  114. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  115. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  116. };
  117. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  118. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  119. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  120. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  121. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  122. };
  123. void pc_set_reg_load( bool val ){
  124. reg_load = val;
  125. }
  126. /**
  127. * Item Cool Down Delay Saving
  128. * Struct item_cd is not a member of struct map_session_data
  129. * to keep cooldowns in memory between player log-ins.
  130. * All cooldowns are reset when server is restarted.
  131. **/
  132. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  133. struct item_cd {
  134. unsigned int tick[MAX_ITEMDELAYS]; //tick
  135. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  136. };
  137. /**
  138. * Converts a class to its array index for CLASS_COUNT defined arrays.
  139. * Note that it does not do a validity check for speed purposes, where parsing
  140. * player input make sure to use a pcdb_checkid first!
  141. * @param class_ Job ID see enum e_job
  142. * @return Class Index
  143. */
  144. int pc_class2idx(int class_) {
  145. if (class_ >= JOB_NOVICE_HIGH)
  146. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  147. return class_;
  148. }
  149. /**
  150. * Get player's group ID
  151. * @param sd
  152. * @return Group ID
  153. */
  154. int pc_get_group_id(struct map_session_data *sd) {
  155. return sd->group_id;
  156. }
  157. /** Get player's group Level
  158. * @param sd
  159. * @return Group Level
  160. */
  161. int pc_get_group_level(struct map_session_data *sd) {
  162. return sd->group_level;
  163. }
  164. static TIMER_FUNC(pc_invincible_timer){
  165. struct map_session_data *sd;
  166. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  167. return 1;
  168. if(sd->invincible_timer != tid){
  169. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  170. return 0;
  171. }
  172. sd->invincible_timer = INVALID_TIMER;
  173. skill_unit_move(&sd->bl,tick,1);
  174. return 0;
  175. }
  176. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  177. nullpo_retv(sd);
  178. if( sd->invincible_timer != INVALID_TIMER )
  179. delete_timer(sd->invincible_timer,pc_invincible_timer);
  180. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  181. }
  182. void pc_delinvincibletimer(struct map_session_data* sd)
  183. {
  184. nullpo_retv(sd);
  185. if( sd->invincible_timer != INVALID_TIMER )
  186. {
  187. delete_timer(sd->invincible_timer,pc_invincible_timer);
  188. sd->invincible_timer = INVALID_TIMER;
  189. skill_unit_move(&sd->bl,gettick(),1);
  190. }
  191. }
  192. static TIMER_FUNC(pc_spiritball_timer){
  193. struct map_session_data *sd;
  194. int i;
  195. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  196. return 1;
  197. if( sd->spiritball <= 0 )
  198. {
  199. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  200. sd->spiritball = 0;
  201. return 0;
  202. }
  203. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  204. if( i == sd->spiritball )
  205. {
  206. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  207. return 0;
  208. }
  209. sd->spiritball--;
  210. if( i != sd->spiritball )
  211. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  212. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  213. clif_spiritball(&sd->bl);
  214. return 0;
  215. }
  216. /**
  217. * Adds a spiritball to player for 'interval' ms
  218. * @param sd
  219. * @param interval
  220. * @param max
  221. */
  222. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  223. {
  224. int tid;
  225. uint8 i;
  226. nullpo_retv(sd);
  227. if(max > MAX_SPIRITBALL)
  228. max = MAX_SPIRITBALL;
  229. if(sd->spiritball < 0)
  230. sd->spiritball = 0;
  231. if( sd->spiritball && sd->spiritball >= max )
  232. {
  233. if(sd->spirit_timer[0] != INVALID_TIMER)
  234. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  235. sd->spiritball--;
  236. if( sd->spiritball != 0 )
  237. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  238. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  239. }
  240. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  241. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  242. if( i != sd->spiritball )
  243. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  244. sd->spirit_timer[i] = tid;
  245. sd->spiritball++;
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. /**
  252. * Removes number of spiritball from player
  253. * @param sd
  254. * @param count
  255. * @param type 1 = doesn't give client effect
  256. */
  257. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  258. {
  259. uint8 i;
  260. nullpo_retv(sd);
  261. if(sd->spiritball <= 0) {
  262. sd->spiritball = 0;
  263. return;
  264. }
  265. if(count == 0)
  266. return;
  267. if(count > sd->spiritball)
  268. count = sd->spiritball;
  269. sd->spiritball -= count;
  270. if(count > MAX_SPIRITBALL)
  271. count = MAX_SPIRITBALL;
  272. for(i=0;i<count;i++) {
  273. if(sd->spirit_timer[i] != INVALID_TIMER) {
  274. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  275. sd->spirit_timer[i] = INVALID_TIMER;
  276. }
  277. }
  278. for(i=count;i<MAX_SPIRITBALL;i++) {
  279. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  280. sd->spirit_timer[i] = INVALID_TIMER;
  281. }
  282. if(!type) {
  283. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  284. clif_millenniumshield(&sd->bl,sd->spiritball);
  285. else
  286. clif_spiritball(&sd->bl);
  287. }
  288. }
  289. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  290. int *c, *b_sd;
  291. struct block_list *src;
  292. struct map_session_data *tsd;
  293. struct status_change *sc;
  294. nullpo_ret(bl);
  295. nullpo_ret(tsd = (struct map_session_data*)bl);
  296. nullpo_ret(src = va_arg(ap,struct block_list *));
  297. c = va_arg(ap,int *);
  298. b_sd = va_arg(ap, int *);
  299. if(pc_isdead(tsd))
  300. return 0;
  301. sc = status_get_sc(bl);
  302. if( sc && sc->data[SC_BANDING] )
  303. {
  304. b_sd[(*c)++] = tsd->bl.id;
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  310. int c;
  311. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  312. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  313. int range = skill_get_splash(LG_BANDING,skill_lv);
  314. nullpo_ret(sd);
  315. c = 0;
  316. memset(b_sd, 0, sizeof(b_sd));
  317. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  318. if( c < 1 ) //just recalc status no need to recalc hp
  319. { // No more Royal Guards in Banding found.
  320. struct status_change *sc;
  321. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  322. {
  323. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  324. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  325. }
  326. return 0;
  327. }
  328. //Add yourself
  329. hp = status_get_hp(&sd->bl);
  330. i++;
  331. // Get total HP of all Royal Guards in party.
  332. for( j = 0; j < i; j++ ){
  333. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  334. if( bsd != NULL )
  335. hp += status_get_hp(&bsd->bl);
  336. }
  337. // Set average HP.
  338. hp = hp / i;
  339. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  340. for( j = 0; j < i; j++ )
  341. {
  342. int tmp_hp=0;
  343. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  344. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  345. extra_hp += tmp_hp;
  346. tmp_qty++;
  347. }
  348. }
  349. if( extra_hp > 0 && tmp_qty > 0 )
  350. hp += extra_hp / tmp_qty;
  351. for( j = 0; j < i; j++ ){
  352. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  353. if( bsd != NULL )
  354. {
  355. struct status_change *sc;
  356. status_set_hp(&bsd->bl,hp,0); // Set hp
  357. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  358. {
  359. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  360. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  361. }
  362. }
  363. }
  364. return c;
  365. }
  366. /**
  367. * Increases a player's fame points and displays a notice to him
  368. * @param sd Player
  369. * @param count Fame point
  370. */
  371. void pc_addfame(struct map_session_data *sd,int count)
  372. {
  373. enum e_rank ranktype;
  374. nullpo_retv(sd);
  375. sd->status.fame += count;
  376. if(sd->status.fame > MAX_FAME)
  377. sd->status.fame = MAX_FAME;
  378. switch(sd->class_&MAPID_UPPERMASK){
  379. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  380. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  381. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  382. default:
  383. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  384. return;
  385. }
  386. clif_update_rankingpoint(sd,ranktype,count);
  387. chrif_updatefamelist(sd);
  388. }
  389. /**
  390. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  391. * @param sd
  392. * @param job Job use enum e_mapid
  393. * @return Rank
  394. */
  395. unsigned char pc_famerank(uint32 char_id, int job)
  396. {
  397. uint8 i;
  398. switch(job){
  399. case MAPID_BLACKSMITH: // Blacksmith
  400. for(i = 0; i < MAX_FAME_LIST; i++){
  401. if(smith_fame_list[i].id == char_id)
  402. return i + 1;
  403. }
  404. break;
  405. case MAPID_ALCHEMIST: // Alchemist
  406. for(i = 0; i < MAX_FAME_LIST; i++){
  407. if(chemist_fame_list[i].id == char_id)
  408. return i + 1;
  409. }
  410. break;
  411. case MAPID_TAEKWON: // Taekwon
  412. for(i = 0; i < MAX_FAME_LIST; i++){
  413. if(taekwon_fame_list[i].id == char_id)
  414. return i + 1;
  415. }
  416. break;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * Restart player's HP & SP value
  422. * @param sd
  423. * @param type Restart type: 1 - Normal Resurection
  424. */
  425. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  426. struct status_data *status, *b_status;
  427. nullpo_retv(sd);
  428. b_status = &sd->base_status;
  429. status = &sd->battle_status;
  430. if (type&1) { //Normal resurrection
  431. status->hp = 1; //Otherwise status_heal may fail if dead.
  432. status_heal(&sd->bl, b_status->hp, 0, 1);
  433. if( status->sp < b_status->sp )
  434. status_set_sp(&sd->bl, b_status->sp, 1);
  435. } else { //Just for saving on the char-server (with values as if respawned)
  436. sd->status.hp = b_status->hp;
  437. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  438. }
  439. }
  440. /*==========================================
  441. Rental System
  442. *------------------------------------------*/
  443. /**
  444. * Ends a rental and removes the item/effect
  445. * @param tid: Tick ID
  446. * @param tick: Timer
  447. * @param id: Timer ID
  448. * @param data: Data
  449. * @return false - failure, true - success
  450. */
  451. TIMER_FUNC(pc_inventory_rental_end){
  452. struct map_session_data *sd = map_id2sd(id);
  453. if( sd == NULL )
  454. return 0;
  455. if( tid != sd->rental_timer ) {
  456. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  457. return 0;
  458. }
  459. pc_inventory_rentals(sd);
  460. return 1;
  461. }
  462. /**
  463. * Removes the rental timer from the player
  464. * @param sd: Player data
  465. */
  466. void pc_inventory_rental_clear(struct map_session_data *sd)
  467. {
  468. if( sd->rental_timer != INVALID_TIMER ) {
  469. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  470. sd->rental_timer = INVALID_TIMER;
  471. }
  472. }
  473. /**
  474. * Check for items in the player's inventory that are rental type
  475. * @param sd: Player data
  476. */
  477. void pc_inventory_rentals(struct map_session_data *sd)
  478. {
  479. int i, c = 0;
  480. unsigned int next_tick = UINT_MAX;
  481. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  482. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  483. continue; // Nothing here
  484. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  485. continue;
  486. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  487. if (sd->inventory_data[i]->unequip_script)
  488. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  489. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  490. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  491. } else {
  492. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  493. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  494. next_tick = umin(expire_tick * 1000U, next_tick);
  495. c++;
  496. }
  497. }
  498. 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
  499. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  500. else
  501. sd->rental_timer = INVALID_TIMER;
  502. }
  503. /**
  504. * Add a rental item to the player and adjusts the rental timer appropriately
  505. * @param sd: Player data
  506. * @param seconds: Rental time
  507. */
  508. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  509. {
  510. unsigned int tick = seconds * 1000;
  511. if( sd == NULL )
  512. return;
  513. if( sd->rental_timer != INVALID_TIMER ) {
  514. const struct TimerData * td;
  515. td = get_timer(sd->rental_timer);
  516. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  517. pc_inventory_rental_clear(sd);
  518. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  519. }
  520. } else
  521. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  522. }
  523. /**
  524. * Check if the player can sell the current item
  525. * @param sd: map_session_data of the player
  526. * @param item: struct of the checking item
  527. * @return bool 'true' is sellable, 'false' otherwise
  528. */
  529. bool pc_can_sell_item(struct map_session_data *sd, struct item *item) {
  530. struct npc_data *nd;
  531. if (sd == NULL || item == NULL)
  532. return false;
  533. nd = map_id2nd(sd->npc_shopid);
  534. if (battle_config.hide_fav_sell && item->favorite)
  535. return false; //Cannot sell favs (optional config)
  536. if (item->expire_time)
  537. return false; // Cannot Sell Rental Items
  538. if (nd && nd->subtype == NPCTYPE_ITEMSHOP) {
  539. struct item_data *itd;
  540. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND)
  541. return true; // NPCTYPE_ITEMSHOP and bound item config is sellable
  542. if ((itd = itemdb_search(item->nameid)) && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  543. return true; // NPCTYPE_ITEMSHOP and sell restricted item config is sellable
  544. }
  545. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  546. return false;
  547. if (item->bound && !pc_can_give_bounded_items(sd))
  548. return false; // Don't allow sale of bound items
  549. return true;
  550. }
  551. /**
  552. * Determines if player can give / drop / trade / vend items
  553. */
  554. bool pc_can_give_items(struct map_session_data *sd)
  555. {
  556. return pc_has_permission(sd, PC_PERM_TRADE);
  557. }
  558. /**
  559. * Determines if player can give / drop / trade / vend bounded items
  560. */
  561. bool pc_can_give_bounded_items(struct map_session_data *sd)
  562. {
  563. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  564. }
  565. /*==========================================
  566. * Prepares character for saving.
  567. * @param sd
  568. *------------------------------------------*/
  569. void pc_makesavestatus(struct map_session_data *sd) {
  570. nullpo_retv(sd);
  571. if(!battle_config.save_clothcolor)
  572. sd->status.clothes_color = 0;
  573. // Since this is currently not officially released,
  574. // its best to have a forced option to not save body styles.
  575. if(!battle_config.save_body_style)
  576. sd->status.body = 0;
  577. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  578. //status change load/saving. [Skotlex]
  579. #ifdef NEW_CARTS
  580. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  581. #else
  582. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  583. #endif
  584. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  585. if(pc_isdead(sd)){
  586. pc_setrestartvalue(sd, 0);
  587. } else {
  588. sd->status.hp = sd->battle_status.hp;
  589. sd->status.sp = sd->battle_status.sp;
  590. }
  591. sd->status.last_point.map = sd->mapindex;
  592. sd->status.last_point.x = sd->bl.x;
  593. sd->status.last_point.y = sd->bl.y;
  594. return;
  595. }
  596. if(pc_isdead(sd)) {
  597. pc_setrestartvalue(sd, 0);
  598. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  599. } else {
  600. sd->status.hp = sd->battle_status.hp;
  601. sd->status.sp = sd->battle_status.sp;
  602. sd->status.last_point.map = sd->mapindex;
  603. sd->status.last_point.x = sd->bl.x;
  604. sd->status.last_point.y = sd->bl.y;
  605. }
  606. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  607. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  608. if(mapdata->save.map)
  609. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  610. else
  611. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  612. }
  613. }
  614. /*==========================================
  615. * Off init ? Connection?
  616. *------------------------------------------*/
  617. 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) {
  618. nullpo_retv(sd);
  619. sd->bl.id = account_id;
  620. sd->status.account_id = account_id;
  621. sd->status.char_id = char_id;
  622. sd->status.sex = sex;
  623. sd->login_id1 = login_id1;
  624. sd->login_id2 = 0; // at this point, we can not know the value :(
  625. sd->client_tick = client_tick;
  626. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  627. sd->bl.type = BL_PC;
  628. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  629. sd->canlog_tick = gettick();
  630. //Required to prevent homunculus copuing a base speed of 0.
  631. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  632. }
  633. /**
  634. * Get equip point for an equip
  635. * @param sd
  636. * @param id
  637. */
  638. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  639. int ep = 0;
  640. nullpo_ret(sd);
  641. nullpo_ret(id);
  642. if (!itemdb_isequip2(id))
  643. return 0; //Not equippable by players.
  644. ep = id->equip;
  645. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  646. if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
  647. if (ep == EQP_WEAPON)
  648. return EQP_ARMS;
  649. if (ep == EQP_SHADOW_WEAPON)
  650. return EQP_SHADOW_ARMS;
  651. }
  652. }
  653. return ep;
  654. }
  655. /**
  656. * Get equip point for an equip
  657. * @param sd
  658. * @param n Equip index in inventory
  659. */
  660. int pc_equippoint(struct map_session_data *sd,int n){
  661. nullpo_ret(sd);
  662. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  663. }
  664. /**
  665. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  666. * @param sd : player session
  667. * @return 0 sucess, 1:invalid sd
  668. */
  669. void pc_setinventorydata(struct map_session_data *sd)
  670. {
  671. uint8 i;
  672. nullpo_retv(sd);
  673. for(i = 0; i < MAX_INVENTORY; i++) {
  674. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  675. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  676. }
  677. }
  678. /**
  679. * 'Calculates' weapon type
  680. * @param sd : Player
  681. */
  682. void pc_calcweapontype(struct map_session_data *sd)
  683. {
  684. nullpo_retv(sd);
  685. // single-hand
  686. if(sd->weapontype2 == W_FIST) {
  687. sd->status.weapon = sd->weapontype1;
  688. return;
  689. }
  690. if(sd->weapontype1 == W_FIST) {
  691. sd->status.weapon = sd->weapontype2;
  692. return;
  693. }
  694. // dual-wield
  695. sd->status.weapon = 0;
  696. switch (sd->weapontype1){
  697. case W_DAGGER:
  698. switch (sd->weapontype2) {
  699. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  700. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  701. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  702. }
  703. break;
  704. case W_1HSWORD:
  705. switch (sd->weapontype2) {
  706. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  707. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  708. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  709. }
  710. break;
  711. case W_1HAXE:
  712. switch (sd->weapontype2) {
  713. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  714. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  715. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  716. }
  717. }
  718. // unknown, default to right hand type
  719. if (!sd->status.weapon)
  720. sd->status.weapon = sd->weapontype1;
  721. }
  722. /**
  723. * Set equip index
  724. * @param sd : Player
  725. */
  726. void pc_setequipindex(struct map_session_data *sd)
  727. {
  728. uint16 i;
  729. nullpo_retv(sd);
  730. for (i = 0; i < EQI_MAX; i++)
  731. sd->equip_index[i] = -1;
  732. for (i = 0; i < MAX_INVENTORY; i++) {
  733. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  734. continue;
  735. if (sd->inventory.u.items_inventory[i].equip) {
  736. uint8 j;
  737. for (j = 0; j < EQI_MAX; j++)
  738. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  739. sd->equip_index[j] = i;
  740. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  741. if (sd->inventory_data[i])
  742. sd->weapontype1 = sd->inventory_data[i]->look;
  743. else
  744. sd->weapontype1 = 0;
  745. }
  746. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  747. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  748. sd->weapontype2 = sd->inventory_data[i]->look;
  749. else
  750. sd->weapontype2 = 0;
  751. }
  752. }
  753. }
  754. pc_calcweapontype(sd);
  755. }
  756. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  757. //{
  758. // int i;
  759. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  760. // struct item_data *data;
  761. //
  762. // //Crafted/made/hatched items.
  763. // if (itemdb_isspecial(item->card[0]))
  764. // return 1;
  765. //
  766. // /* scan for enchant armor gems */
  767. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  768. // s = MAX_SLOTS - 1;
  769. //
  770. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  771. // return( i < s ) ? 0 : 1;
  772. //}
  773. /**
  774. * Check if an item is equiped by player
  775. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  776. * @param sd : player session
  777. * @param nameid : itemid
  778. * @return 1:yes, 0:no
  779. */
  780. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  781. {
  782. uint8 i;
  783. for( i = 0; i < EQI_MAX; i++ )
  784. {
  785. short index = sd->equip_index[i], j;
  786. if( index < 0 )
  787. continue;
  788. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  789. continue;
  790. if( !sd->inventory_data[index] )
  791. continue;
  792. if( sd->inventory_data[index]->nameid == nameid )
  793. return true;
  794. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  795. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  796. return true;
  797. }
  798. }
  799. return false;
  800. }
  801. /**
  802. * Check adoption rules
  803. * @param p1_sd: Player 1
  804. * @param p2_sd: Player 2
  805. * @param b_sd: Player that will be adopted
  806. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  807. * ADOPT_ALREADY_ADOPTED - Already adopted
  808. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  809. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  810. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  811. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  812. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  813. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  814. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  815. */
  816. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  817. {
  818. if( !p1_sd || !p2_sd || !b_sd )
  819. return ADOPT_CHARACTER_NOT_FOUND;
  820. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  821. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  822. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  823. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  824. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  825. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  826. if( p2_sd->status.party_id != p1_sd->status.party_id )
  827. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  828. // Parents need to have their ring equipped
  829. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  830. return ADOPT_EQUIP_RINGS;
  831. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  832. return ADOPT_EQUIP_RINGS;
  833. // Already adopted a baby
  834. if( p1_sd->status.child || p2_sd->status.child ) {
  835. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  836. return ADOPT_MORE_CHILDREN;
  837. }
  838. // Parents need at least lvl 70 to adopt
  839. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  840. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  841. return ADOPT_LEVEL_70;
  842. }
  843. if( b_sd->status.partner_id ) {
  844. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  845. return ADOPT_MARRIED;
  846. }
  847. 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 ) )
  848. return ADOPT_NOT_NOVICE;
  849. return ADOPT_ALLOWED;
  850. }
  851. /*==========================================
  852. * Adoption Process
  853. *------------------------------------------*/
  854. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  855. {
  856. int job, joblevel;
  857. unsigned int jobexp;
  858. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  859. return false;
  860. // Preserve current job levels and progress
  861. joblevel = b_sd->status.job_level;
  862. jobexp = b_sd->status.job_exp;
  863. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  864. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  865. { // Success, proceed to configure parents and baby skills
  866. p1_sd->status.child = b_sd->status.char_id;
  867. p2_sd->status.child = b_sd->status.char_id;
  868. b_sd->status.father = p1_sd->status.char_id;
  869. b_sd->status.mother = p2_sd->status.char_id;
  870. // Restore progress
  871. b_sd->status.job_level = joblevel;
  872. clif_updatestatus(b_sd, SP_JOBLEVEL);
  873. b_sd->status.job_exp = jobexp;
  874. clif_updatestatus(b_sd, SP_JOBEXP);
  875. // Baby Skills
  876. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  877. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  878. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  879. // Parents Skills
  880. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  881. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  882. chrif_save(p1_sd, CSAVE_NORMAL);
  883. chrif_save(p2_sd, CSAVE_NORMAL);
  884. chrif_save(b_sd, CSAVE_NORMAL);
  885. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  886. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  887. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  888. return true;
  889. }
  890. return false; // Job Change Fail
  891. }
  892. /*==========================================
  893. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  894. Returns:
  895. false : Cannot use/equip
  896. true : Can use/equip
  897. * Credits:
  898. [Inkfish] for first idea
  899. [Haru] for third-classes extension
  900. [Cydh] finishing :D
  901. *------------------------------------------*/
  902. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  903. while (1) {
  904. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  905. break;
  906. #ifndef RENEWAL
  907. //allow third classes to use trans. class items
  908. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  909. break;
  910. //third-baby classes can use same item too
  911. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  912. break;
  913. //don't need to decide specific rules for third-classes?
  914. //items for third classes can be used for all third classes
  915. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  916. break;
  917. #else
  918. //trans. classes (exl. third-trans.)
  919. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  920. break;
  921. //baby classes (exl. third-baby)
  922. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  923. break;
  924. //third classes (exl. third-trans. and baby-third)
  925. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  926. break;
  927. //trans-third classes
  928. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  929. break;
  930. //third-baby classes
  931. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  932. break;
  933. #endif
  934. return false;
  935. }
  936. return true;
  937. }
  938. /*=================================================
  939. * Checks if the player can equip the item at index n in inventory.
  940. * @param sd
  941. * @param n Item index in inventory
  942. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  943. *------------------------------------------------*/
  944. uint8 pc_isequip(struct map_session_data *sd,int n)
  945. {
  946. struct item_data *item;
  947. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  948. item = sd->inventory_data[n];
  949. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  950. return ITEM_EQUIP_ACK_OK;
  951. if(item == NULL)
  952. return ITEM_EQUIP_ACK_FAIL;
  953. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  954. return ITEM_EQUIP_ACK_FAILLEVEL;
  955. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  956. return ITEM_EQUIP_ACK_FAILLEVEL;
  957. if(item->sex != 2 && sd->status.sex != item->sex)
  958. return ITEM_EQUIP_ACK_FAIL;
  959. //fail to equip if item is restricted
  960. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  961. return ITEM_EQUIP_ACK_FAIL;
  962. if (sd->sc.count) {
  963. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  964. return ITEM_EQUIP_ACK_FAIL;
  965. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  966. return ITEM_EQUIP_ACK_FAIL;
  967. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  968. return ITEM_EQUIP_ACK_FAIL;
  969. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  970. return ITEM_EQUIP_ACK_FAIL;
  971. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  972. return ITEM_EQUIP_ACK_FAIL;
  973. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  974. return ITEM_EQUIP_ACK_FAIL;
  975. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  976. return ITEM_EQUIP_ACK_FAIL;
  977. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  978. //Spirit of Super Novice equip bonuses. [Skotlex]
  979. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  980. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  981. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  982. switch(item->look) { //In weapons, the look determines type of weapon.
  983. case W_DAGGER: //All level 4 - Daggers
  984. case W_1HSWORD: //All level 4 - 1H Swords
  985. case W_1HAXE: //All level 4 - 1H Axes
  986. case W_MACE: //All level 4 - 1H Maces
  987. case W_STAFF: //All level 4 - 1H Staves
  988. case W_2HSTAFF: //All level 4 - 2H Staves
  989. return ITEM_EQUIP_ACK_OK;
  990. }
  991. }
  992. }
  993. //Not equipable by class. [Skotlex]
  994. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  995. return ITEM_EQUIP_ACK_FAIL;
  996. if (!pc_isItemClass(sd, item))
  997. return ITEM_EQUIP_ACK_FAIL;
  998. return ITEM_EQUIP_ACK_OK;
  999. }
  1000. /*==========================================
  1001. * No problem with the session id
  1002. * set the status that has been sent from char server
  1003. *------------------------------------------*/
  1004. 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)
  1005. {
  1006. int i;
  1007. unsigned long tick = gettick();
  1008. uint32 ip = session[sd->fd]->client_addr;
  1009. sd->login_id2 = login_id2;
  1010. sd->group_id = group_id;
  1011. /* load user permissions */
  1012. pc_group_pc_load(sd);
  1013. memcpy(&sd->status, st, sizeof(*st));
  1014. if (st->sex != sd->status.sex) {
  1015. clif_authfail_fd(sd->fd, 0);
  1016. return false;
  1017. }
  1018. //Set the map-server used job id. [Skotlex]
  1019. i = pc_jobid2mapid(sd->status.class_);
  1020. if (i == -1) { //Invalid class?
  1021. 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);
  1022. sd->status.class_ = JOB_NOVICE;
  1023. sd->class_ = MAPID_NOVICE;
  1024. } else
  1025. sd->class_ = i;
  1026. // Checks and fixes to character status data, that are required
  1027. // in case of configuration change or stuff, which cannot be
  1028. // checked on char-server.
  1029. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1030. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1031. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1032. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1033. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1034. sd->state.connect_new = 1;
  1035. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1036. sd->invincible_timer = INVALID_TIMER;
  1037. sd->npc_timer_id = INVALID_TIMER;
  1038. sd->pvp_timer = INVALID_TIMER;
  1039. sd->expiration_tid = INVALID_TIMER;
  1040. sd->autotrade_tid = INVALID_TIMER;
  1041. #ifdef SECURE_NPCTIMEOUT
  1042. // Initialize to defaults/expected
  1043. sd->npc_idle_timer = INVALID_TIMER;
  1044. sd->npc_idle_tick = tick;
  1045. sd->npc_idle_type = NPCT_INPUT;
  1046. sd->state.ignoretimeout = false;
  1047. #endif
  1048. sd->canuseitem_tick = tick;
  1049. sd->canusecashfood_tick = tick;
  1050. sd->canequip_tick = tick;
  1051. sd->cantalk_tick = tick;
  1052. sd->canskill_tick = tick;
  1053. sd->cansendmail_tick = tick;
  1054. sd->idletime = last_tick;
  1055. for(i = 0; i < MAX_SPIRITBALL; i++)
  1056. sd->spirit_timer[i] = INVALID_TIMER;
  1057. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  1058. sd->autobonus[i].active = INVALID_TIMER;
  1059. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  1060. sd->autobonus2[i].active = INVALID_TIMER;
  1061. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  1062. sd->autobonus3[i].active = INVALID_TIMER;
  1063. if (battle_config.item_auto_get)
  1064. sd->state.autoloot = 10000;
  1065. if (battle_config.disp_experience)
  1066. sd->state.showexp = 1;
  1067. if (battle_config.disp_zeny)
  1068. sd->state.showzeny = 1;
  1069. #ifdef VIP_ENABLE
  1070. if (!battle_config.vip_disp_rate)
  1071. sd->vip.disableshowrate = 1;
  1072. #endif
  1073. if (!(battle_config.display_skill_fail&2))
  1074. sd->state.showdelay = 1;
  1075. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1076. memset(&sd->cart, 0, sizeof(struct s_storage));
  1077. memset(&sd->storage, 0, sizeof(struct s_storage));
  1078. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1079. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1080. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1081. sd->status.option &= ~OPTION_INVISIBLE;
  1082. }
  1083. status_change_init(&sd->bl);
  1084. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1085. unit_dataset(&sd->bl);
  1086. sd->guild_x = -1;
  1087. sd->guild_y = -1;
  1088. sd->delayed_damage = 0;
  1089. // Event Timers
  1090. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1091. sd->eventtimer[i] = INVALID_TIMER;
  1092. // Rental Timer
  1093. sd->rental_timer = INVALID_TIMER;
  1094. for( i = 0; i < 3; i++ )
  1095. sd->hate_mob[i] = -1;
  1096. sd->quest_log = NULL;
  1097. sd->num_quests = 0;
  1098. sd->avail_quests = 0;
  1099. sd->save_quest = false;
  1100. sd->count_rewarp = 0;
  1101. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1102. sd->regs.arrays = NULL;
  1103. sd->vars_dirty = false;
  1104. sd->vars_ok = false;
  1105. sd->vars_received = 0x0;
  1106. sd->qi_display = NULL;
  1107. sd->qi_count = 0;
  1108. //warp player
  1109. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1110. 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);
  1111. // try warping to a default map instead (church graveyard)
  1112. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1113. // if we fail again
  1114. clif_authfail_fd(sd->fd, 0);
  1115. return false;
  1116. }
  1117. }
  1118. clif_authok(sd);
  1119. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1120. sd->die_counter=-1;
  1121. //display login notice
  1122. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1123. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1124. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1125. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1126. sd->status.name, sd->status.account_id, sd->status.char_id,
  1127. CONVIP(ip), sd->group_id);
  1128. // Send friends list
  1129. clif_friendslist_send(sd);
  1130. if( !changing_mapservers ) {
  1131. if (battle_config.display_version == 1)
  1132. pc_show_version(sd);
  1133. // Message of the Day [Valaris]
  1134. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1135. if (battle_config.motd_type)
  1136. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1137. else
  1138. clif_displaymessage(sd->fd, motd_text[i]);
  1139. }
  1140. if (expiration_time != 0)
  1141. sd->expiration_time = expiration_time;
  1142. /**
  1143. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1144. **/
  1145. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1146. }
  1147. pc_validate_skill(sd);
  1148. /* [Ind] */
  1149. sd->sc_display = NULL;
  1150. sd->sc_display_count = 0;
  1151. // Player has not yet received the CashShop list
  1152. sd->status.cashshop_sent = false;
  1153. sd->last_addeditem_index = -1;
  1154. sd->bonus_script.head = NULL;
  1155. sd->bonus_script.count = 0;
  1156. #if PACKETVER >= 20150513
  1157. sd->hatEffectIDs = NULL;
  1158. sd->hatEffectCount = 0;
  1159. #endif
  1160. sd->catch_target_class = PET_CATCH_FAIL;
  1161. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1162. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1163. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1164. clif_updatestatus(sd, SP_BASEEXP);
  1165. }
  1166. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1167. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1168. clif_updatestatus(sd, SP_JOBEXP);
  1169. }
  1170. // Request all registries (auth is considered completed whence they arrive)
  1171. intif_request_registry(sd,7);
  1172. return true;
  1173. }
  1174. /*==========================================
  1175. * Closes a connection because it failed to be authenticated from the char server.
  1176. *------------------------------------------*/
  1177. void pc_authfail(struct map_session_data *sd)
  1178. {
  1179. clif_authfail_fd(sd->fd, 0);
  1180. return;
  1181. }
  1182. /**
  1183. * Player register a bl as hatred
  1184. * @param sd : player session
  1185. * @param pos : hate position [0;2]
  1186. * @param bl : target bl
  1187. * @return false:failed, true:success
  1188. */
  1189. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1190. {
  1191. int class_;
  1192. if (!sd || !bl || pos < 0 || pos > 2)
  1193. return false;
  1194. if (sd->hate_mob[pos] != -1)
  1195. { //Can't change hate targets.
  1196. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1197. return false;
  1198. }
  1199. class_ = status_get_class(bl);
  1200. if (!pcdb_checkid(class_)) {
  1201. unsigned int max_hp = status_get_max_hp(bl);
  1202. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1203. return false;
  1204. if (pos != status_get_size(bl))
  1205. return false; //Wrong size
  1206. }
  1207. sd->hate_mob[pos] = class_;
  1208. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1209. clif_hate_info(sd, pos, class_, 1);
  1210. return true;
  1211. }
  1212. /*==========================================
  1213. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1214. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1215. * See intif_parse_StorageReceived() for item operations [lighta]
  1216. *------------------------------------------*/
  1217. void pc_reg_received(struct map_session_data *sd)
  1218. {
  1219. uint8 i;
  1220. sd->vars_ok = true;
  1221. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1222. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1223. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1224. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1225. if (msg_checklangtype(sd->langtype,true) < 0)
  1226. sd->langtype = 0; //invalid langtype reset to default
  1227. // Cash shop
  1228. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1229. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1230. // Cooking Exp
  1231. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1232. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1233. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1234. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1235. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1236. }
  1237. if (battle_config.feature_banking)
  1238. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1239. if (battle_config.feature_roulette) {
  1240. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1241. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1242. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1243. }
  1244. sd->roulette.prizeIdx = -1;
  1245. //SG map and mob read [Komurka]
  1246. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1247. uint16 j;
  1248. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1249. sd->feel_map[i].index = j;
  1250. sd->feel_map[i].m = map_mapindex2mapid(j);
  1251. } else {
  1252. sd->feel_map[i].index = 0;
  1253. sd->feel_map[i].m = -1;
  1254. }
  1255. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1256. }
  1257. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1258. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1259. sd->cloneskill_idx = skill_get_index(skid);
  1260. if (sd->cloneskill_idx > 0) {
  1261. sd->status.skill[sd->cloneskill_idx].id = skid;
  1262. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1263. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1264. sd->status.skill[sd->cloneskill_idx].lv = i;
  1265. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1266. }
  1267. }
  1268. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1269. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1270. sd->reproduceskill_idx = skill_get_index(skid);
  1271. if (sd->reproduceskill_idx > 0) {
  1272. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1273. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1274. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1275. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1276. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1277. }
  1278. }
  1279. //Weird... maybe registries were reloaded?
  1280. if (sd->state.active)
  1281. return;
  1282. sd->state.active = 1;
  1283. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1284. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1285. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1286. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1287. if (sd->status.party_id)
  1288. party_member_joined(sd);
  1289. if (sd->status.guild_id)
  1290. guild_member_joined(sd);
  1291. if( sd->status.clan_id )
  1292. clan_member_joined(sd);
  1293. // pet
  1294. if (sd->status.pet_id > 0)
  1295. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1296. // Homunculus [albator]
  1297. if( sd->status.hom_id > 0 )
  1298. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1299. if( sd->status.mer_id > 0 )
  1300. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1301. if( sd->status.ele_id > 0 )
  1302. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1303. map_addiddb(&sd->bl);
  1304. map_delnickdb(sd->status.char_id, sd->status.name);
  1305. if (!chrif_auth_finished(sd))
  1306. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1307. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1308. chrif_bsdata_request(sd->status.char_id);
  1309. #ifdef VIP_ENABLE
  1310. sd->vip.time = 0;
  1311. sd->vip.enabled = 0;
  1312. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1313. #endif
  1314. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1315. intif_request_questlog(sd);
  1316. if (battle_config.feature_achievement) {
  1317. sd->achievement_data.total_score = 0;
  1318. sd->achievement_data.level = 0;
  1319. sd->achievement_data.save = false;
  1320. sd->achievement_data.count = 0;
  1321. sd->achievement_data.incompleteCount = 0;
  1322. sd->achievement_data.achievements = NULL;
  1323. intif_request_achievements(sd->status.char_id);
  1324. }
  1325. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1326. sd->state.connect_new = 1;
  1327. clif_parse_LoadEndAck(sd->fd, sd);
  1328. }
  1329. if( pc_isinvisible(sd) ) {
  1330. sd->vd.class_ = JT_INVISIBLE;
  1331. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1332. // decrement the number of pvp players on the map
  1333. map_getmapdata(sd->bl.m)->users_pvp--;
  1334. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1335. // unregister the player for ranking
  1336. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1337. sd->pvp_timer = INVALID_TIMER;
  1338. }
  1339. clif_changeoption( &sd->bl );
  1340. }
  1341. channel_autojoin(sd);
  1342. }
  1343. static int pc_calc_skillpoint(struct map_session_data* sd)
  1344. {
  1345. uint16 i, skill_point = 0;
  1346. nullpo_ret(sd);
  1347. for(i = 1; i < MAX_SKILL; i++) {
  1348. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1349. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1350. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1351. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1352. )
  1353. {
  1354. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1355. skill_point += sd->status.skill[i].lv;
  1356. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1357. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1358. }
  1359. }
  1360. }
  1361. return skill_point;
  1362. }
  1363. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1364. uint16 i = 0;
  1365. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1366. return false;
  1367. /**
  1368. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1369. * skill tree and since they have no icons they'll give resource errors
  1370. * Get ALL skills except npc/guild ones. [Skotlex]
  1371. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1372. **/
  1373. for( i = 0; i < MAX_SKILL; i++ ) {
  1374. uint16 skill_id = skill_idx2id(i);
  1375. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1376. continue;
  1377. switch (skill_id) {
  1378. case SM_SELFPROVOKE:
  1379. case AB_DUPLELIGHT_MELEE:
  1380. case AB_DUPLELIGHT_MAGIC:
  1381. case WL_CHAINLIGHTNING_ATK:
  1382. case WL_TETRAVORTEX_FIRE:
  1383. case WL_TETRAVORTEX_WATER:
  1384. case WL_TETRAVORTEX_WIND:
  1385. case WL_TETRAVORTEX_GROUND:
  1386. case WL_SUMMON_ATK_FIRE:
  1387. case WL_SUMMON_ATK_WIND:
  1388. case WL_SUMMON_ATK_WATER:
  1389. case WL_SUMMON_ATK_GROUND:
  1390. case LG_OVERBRAND_BRANDISH:
  1391. case LG_OVERBRAND_PLUSATK:
  1392. case WM_SEVERE_RAINSTORM_MELEE:
  1393. case RL_R_TRIP_PLUSATK:
  1394. case SG_DEVIL:
  1395. case MO_TRIPLEATTACK:
  1396. case RG_SNATCHER:
  1397. continue;
  1398. default:
  1399. {
  1400. uint8 lv = (uint8)skill_get_max(skill_id);
  1401. if (lv > 0) {
  1402. sd->status.skill[i].id = skill_id;
  1403. if (addlv)
  1404. sd->status.skill[i].lv = lv;
  1405. }
  1406. }
  1407. break;
  1408. }
  1409. }
  1410. return true;
  1411. }
  1412. /*==========================================
  1413. * Calculation of skill level.
  1414. * @param sd
  1415. *------------------------------------------*/
  1416. void pc_calc_skilltree(struct map_session_data *sd)
  1417. {
  1418. int i, flag;
  1419. int c = 0;
  1420. nullpo_retv(sd);
  1421. i = pc_calc_skilltree_normalize_job(sd);
  1422. c = pc_mapid2jobid(i, sd->status.sex);
  1423. if( c == -1 )
  1424. { //Unable to normalize job??
  1425. 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);
  1426. return;
  1427. }
  1428. c = pc_class2idx(c);
  1429. for( i = 0; i < MAX_SKILL; i++ ) {
  1430. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1431. sd->status.skill[i].id = 0; //First clear skills.
  1432. /* permanent skills that must be re-checked */
  1433. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1434. uint16 sk_id = skill_idx2id(i);
  1435. if (!sk_id) {
  1436. sd->status.skill[i].id = 0;
  1437. sd->status.skill[i].lv = 0;
  1438. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1439. continue;
  1440. }
  1441. switch (sk_id) {
  1442. case NV_TRICKDEAD:
  1443. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1444. sd->status.skill[i].id = 0;
  1445. sd->status.skill[i].lv = 0;
  1446. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1447. }
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. for( i = 0; i < MAX_SKILL; i++ ) {
  1453. uint16 skill_id = 0;
  1454. // Restore original level of skills after deleting earned skills.
  1455. 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 ) {
  1456. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1457. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1458. }
  1459. //Enable Bard/Dancer spirit linked skills.
  1460. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1461. continue;
  1462. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1463. //Link Dancer skills to bard.
  1464. if( sd->status.sex ) {
  1465. if( sd->status.skill[i-8].lv < 10 )
  1466. continue;
  1467. sd->status.skill[i].id = skill_id;
  1468. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1469. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1470. }
  1471. //Link Bard skills to dancer.
  1472. else {
  1473. if( sd->status.skill[i].lv < 10 )
  1474. continue;
  1475. sd->status.skill[i-8].id = skill_id - 8;
  1476. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1477. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1478. }
  1479. }
  1480. }
  1481. // Removes Taekwon Ranker skill bonus
  1482. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1483. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1484. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1485. uint16 sk_id = skill_tree[c_][i].skill_id;
  1486. uint16 sk_idx = 0;
  1487. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1488. continue;
  1489. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1490. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1491. continue;
  1492. sd->status.skill[sk_idx].id = 0;
  1493. }
  1494. }
  1495. }
  1496. // Grant all skills
  1497. pc_grant_allskills(sd, false);
  1498. do {
  1499. uint16 skid = 0;
  1500. flag = 0;
  1501. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1502. bool fail = false;
  1503. uint16 sk_idx = skill_get_index(skid);
  1504. if (sd->status.skill[sk_idx].id)
  1505. continue; //Skill already known.
  1506. if (!battle_config.skillfree) {
  1507. uint8 j;
  1508. // Checking required skills
  1509. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1510. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1511. uint16 sk_need_idx = 0;
  1512. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1513. short sk_need = sk_need_id;
  1514. 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)
  1515. sk_need = 0; //Not learned.
  1516. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1517. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1518. else
  1519. sk_need = pc_checkskill(sd,sk_need_id);
  1520. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1521. fail = true;
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1527. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1528. class_ = pc_class2idx(class_);
  1529. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1530. fail = true; // base level requirement wasn't satisfied
  1531. }
  1532. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1533. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1534. class_ = pc_class2idx(class_);
  1535. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1536. fail = true; // job level requirement wasn't satisfied
  1537. }
  1538. }
  1539. if (!fail) {
  1540. int inf2 = skill_get_inf2(skid);
  1541. if (!sd->status.skill[sk_idx].lv && (
  1542. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1543. inf2&INF2_WEDDING_SKILL ||
  1544. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1545. ))
  1546. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1547. sd->status.skill[sk_idx].id = skid;
  1548. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1549. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1550. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1551. }
  1552. flag = 1; // skill list has changed, perform another pass
  1553. }
  1554. }
  1555. } while(flag);
  1556. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1557. unsigned short skid = 0;
  1558. /* Taekwon Ranker Bonus Skill Tree
  1559. ============================================
  1560. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1561. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1562. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1563. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1564. uint16 sk_idx = 0;
  1565. if (!(sk_idx = skill_get_index(skid)))
  1566. continue;
  1567. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1568. continue; //Do not include Quest/Wedding skills.
  1569. if( sd->status.skill[sk_idx].id == 0 ) {
  1570. sd->status.skill[sk_idx].id = skid;
  1571. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1572. } else if( skid != NV_BASIC )
  1573. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1574. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1575. }
  1576. }
  1577. }
  1578. //Checks if you can learn a new skill after having leveled up a skill.
  1579. static void pc_check_skilltree(struct map_session_data *sd)
  1580. {
  1581. int i, flag = 0;
  1582. int c = 0;
  1583. if (battle_config.skillfree)
  1584. return; //Function serves no purpose if this is set
  1585. i = pc_calc_skilltree_normalize_job(sd);
  1586. c = pc_mapid2jobid(i, sd->status.sex);
  1587. if (c == -1) { //Unable to normalize job??
  1588. 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);
  1589. return;
  1590. }
  1591. c = pc_class2idx(c);
  1592. do {
  1593. uint16 skid = 0;
  1594. flag = 0;
  1595. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1596. uint16 sk_idx = skill_get_index(skid);
  1597. bool fail = false;
  1598. uint8 j = 0;
  1599. if (sd->status.skill[sk_idx].id) //Already learned
  1600. continue;
  1601. // Checking required skills
  1602. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1603. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1604. uint16 sk_need_idx = 0;
  1605. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1606. short sk_need = sk_need_id;
  1607. 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)
  1608. sk_need = 0; //Not learned.
  1609. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1610. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1611. else
  1612. sk_need = pc_checkskill(sd,sk_need_id);
  1613. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1614. fail = true;
  1615. break;
  1616. }
  1617. }
  1618. }
  1619. if( fail )
  1620. continue;
  1621. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1622. continue;
  1623. j = skill_get_inf2(skid);
  1624. if( !sd->status.skill[sk_idx].lv && (
  1625. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1626. j&INF2_WEDDING_SKILL ||
  1627. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1628. ) )
  1629. continue; //Cannot be learned via normal means.
  1630. sd->status.skill[sk_idx].id = skid;
  1631. flag = 1;
  1632. }
  1633. } while(flag);
  1634. }
  1635. // Make sure all the skills are in the correct condition
  1636. // before persisting to the backend.. [MouseJstr]
  1637. void pc_clean_skilltree(struct map_session_data *sd)
  1638. {
  1639. uint16 i;
  1640. for (i = 0; i < MAX_SKILL; i++){
  1641. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1642. sd->status.skill[i].id = 0;
  1643. sd->status.skill[i].lv = 0;
  1644. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1645. }
  1646. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1647. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1648. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1649. }
  1650. }
  1651. }
  1652. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1653. {
  1654. int skill_point, novice_skills;
  1655. int c = sd->class_;
  1656. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1657. return c;
  1658. skill_point = pc_calc_skillpoint(sd);
  1659. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1660. // limit 1st class and above to novice job levels
  1661. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1662. {
  1663. c = MAPID_NOVICE;
  1664. }
  1665. // limit 2nd class and above to first class job levels (super novices are exempt)
  1666. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1667. {
  1668. // regenerate change_level_2nd
  1669. if (!sd->change_level_2nd)
  1670. {
  1671. if (sd->class_&JOBL_THIRD)
  1672. {
  1673. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1674. if (!sd->change_level_3rd)
  1675. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1676. else
  1677. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1678. - (sd->status.job_level - 1)
  1679. - (sd->change_level_3rd - 1)
  1680. - novice_skills;
  1681. }
  1682. else
  1683. {
  1684. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1685. - (sd->status.job_level - 1)
  1686. - novice_skills;
  1687. }
  1688. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1689. }
  1690. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1691. {
  1692. c &= MAPID_BASEMASK;
  1693. }
  1694. // limit 3rd class to 2nd class/trans job levels
  1695. else if(sd->class_&JOBL_THIRD)
  1696. {
  1697. // regenerate change_level_3rd
  1698. if (!sd->change_level_3rd)
  1699. {
  1700. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1701. - (sd->status.job_level - 1)
  1702. - (sd->change_level_2nd - 1)
  1703. - novice_skills;
  1704. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1705. }
  1706. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1707. c &= MAPID_UPPERMASK;
  1708. }
  1709. }
  1710. // restore non-limiting flags
  1711. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1712. return c;
  1713. }
  1714. /*==========================================
  1715. * Updates the weight status
  1716. *------------------------------------------
  1717. * 1: overweight 50% for pre-renewal and 70% for renewal
  1718. * 2: overweight 90%
  1719. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1720. */
  1721. void pc_updateweightstatus(struct map_session_data *sd)
  1722. {
  1723. int old_overweight;
  1724. int new_overweight;
  1725. nullpo_retv(sd);
  1726. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1727. #ifdef RENEWAL
  1728. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1729. #else
  1730. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1731. #endif
  1732. if( old_overweight == new_overweight )
  1733. return; // no change
  1734. // stop old status change
  1735. if( old_overweight == 1 )
  1736. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1737. else if( old_overweight == 2 )
  1738. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1739. // start new status change
  1740. if( new_overweight == 1 )
  1741. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1742. else if( new_overweight == 2 )
  1743. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1744. // update overweight status
  1745. sd->regen.state.overweight = new_overweight;
  1746. }
  1747. int pc_disguise(struct map_session_data *sd, int class_)
  1748. {
  1749. if (!class_ && !sd->disguise)
  1750. return 0;
  1751. if (class_ && sd->disguise == class_)
  1752. return 0;
  1753. if(pc_isinvisible(sd))
  1754. { //Character is invisible. Stealth class-change. [Skotlex]
  1755. sd->disguise = class_; //viewdata is set on uncloaking.
  1756. return 2;
  1757. }
  1758. if (sd->bl.prev != NULL) {
  1759. pc_stop_walking(sd, 0);
  1760. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1761. }
  1762. if (!class_) {
  1763. sd->disguise = 0;
  1764. class_ = sd->status.class_;
  1765. } else
  1766. sd->disguise=class_;
  1767. status_set_viewdata(&sd->bl, class_);
  1768. clif_changeoption(&sd->bl);
  1769. if (sd->bl.prev != NULL) {
  1770. clif_spawn(&sd->bl);
  1771. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1772. { //It seems the cart info is lost on undisguise.
  1773. clif_cartlist(sd);
  1774. clif_updatestatus(sd,SP_CARTINFO);
  1775. }
  1776. if (sd->chatID) {
  1777. struct chat_data* cd;
  1778. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1779. clif_dispchat(cd,0);
  1780. }
  1781. }
  1782. return 1;
  1783. }
  1784. /// Show error message
  1785. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1786. /// Check for valid Element, break & show error message if invalid Element
  1787. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1788. /// Check for valid Race, break & show error message if invalid Race
  1789. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1790. /// Check for valid Race2, break & show error message if invalid Race2
  1791. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1792. /// Check for valid Class, break & show error message if invalid Class
  1793. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1794. /// Check for valid Size, break & show error message if invalid Size
  1795. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1796. /// Check for valid SC, break & show error message if invalid SC
  1797. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1798. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1799. {
  1800. uint8 i;
  1801. if( !rate )
  1802. return;
  1803. for( i = 0; i < max && spell[i].id; i++ )
  1804. {
  1805. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1806. {
  1807. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1808. return;
  1809. rate += spell[i].rate;
  1810. break;
  1811. }
  1812. }
  1813. if (i == max) {
  1814. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1815. return;
  1816. }
  1817. spell[i].id = id;
  1818. spell[i].lv = lv;
  1819. spell[i].rate = rate;
  1820. //Auto-update flag value.
  1821. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1822. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1823. if (!(flag&BF_SKILLMASK)) {
  1824. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1825. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1826. }
  1827. spell[i].flag|= flag;
  1828. spell[i].card_id = card_id;
  1829. }
  1830. 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)
  1831. {
  1832. uint8 i;
  1833. if( !rate )
  1834. return;
  1835. for( i = 0; i < max && spell[i].id; i++ )
  1836. {
  1837. ; // each autospell works independently
  1838. }
  1839. if( i == max )
  1840. {
  1841. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1842. return;
  1843. }
  1844. spell[i].flag = src_skill;
  1845. spell[i].id = id;
  1846. spell[i].lv = lv;
  1847. spell[i].rate = rate;
  1848. spell[i].card_id = card_id;
  1849. return;
  1850. }
  1851. /**
  1852. * Add inflict effect bonus for player while attacking/atatcked
  1853. * @param effect Effect array
  1854. * @param pmax Max array
  1855. * @param sc SC/Effect type
  1856. * @param rate Success chance
  1857. * @param arrow_rate success chance if bonus comes from arrow-type item
  1858. * @param flag Target flag
  1859. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1860. **/
  1861. 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)
  1862. {
  1863. uint16 i;
  1864. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1865. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1866. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1867. flag |= ATF_TARGET; //Default target: enemy.
  1868. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1869. flag |= ATF_WEAPON; //Default type: weapon.
  1870. if (!duration)
  1871. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1872. for (i = 0; i < pmax && effect[i].flag; i++) {
  1873. if (effect[i].sc == sc && effect[i].flag == flag) {
  1874. effect[i].rate += rate;
  1875. effect[i].arrow_rate += arrow_rate;
  1876. effect[i].duration = umax(effect[i].duration, duration);
  1877. return;
  1878. }
  1879. }
  1880. if (i == pmax) {
  1881. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1882. return;
  1883. }
  1884. effect[i].sc = sc;
  1885. effect[i].rate = rate;
  1886. effect[i].arrow_rate = arrow_rate;
  1887. effect[i].flag = flag;
  1888. effect[i].duration = duration;
  1889. }
  1890. /**
  1891. * Add inflict effect bonus for player while attacking using skill
  1892. * @param effect Effect array
  1893. * @param pmax Max array
  1894. * @param sc SC/Effect type
  1895. * @param rate Success chance
  1896. * @param flag Target flag
  1897. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1898. **/
  1899. 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)
  1900. {
  1901. uint8 i;
  1902. if (!duration)
  1903. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1904. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1905. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1906. effect[i].rate += rate;
  1907. effect[i].duration = umax(effect[i].duration, duration);
  1908. return;
  1909. }
  1910. }
  1911. if( i == pmax ) {
  1912. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1913. return;
  1914. }
  1915. effect[i].sc = sc;
  1916. effect[i].rate = rate;
  1917. effect[i].skill_id = skill_id;
  1918. effect[i].target = target;
  1919. effect[i].duration = duration;
  1920. }
  1921. /** Adjust/add drop rate modifier for player
  1922. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1923. * @param max: Max bonus can be received
  1924. * @param nameid: item id that will be dropped
  1925. * @param group: group id
  1926. * @param class_: target class
  1927. * @param race: target race. if < 0, means monster_id
  1928. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1929. */
  1930. 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)
  1931. {
  1932. uint8 i;
  1933. if (!nameid && !group) {
  1934. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1935. return;
  1936. }
  1937. if (nameid && !itemdb_exists(nameid)) {
  1938. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1939. return;
  1940. }
  1941. if (group && !itemdb_group_exists(group)) {
  1942. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1943. return;
  1944. }
  1945. //Apply config rate adjustment settings.
  1946. if (rate >= 0) { //Absolute drop.
  1947. if (battle_config.item_rate_adddrop != 100)
  1948. rate = rate*battle_config.item_rate_adddrop/100;
  1949. if (rate < battle_config.item_drop_adddrop_min)
  1950. rate = battle_config.item_drop_adddrop_min;
  1951. else if (rate > battle_config.item_drop_adddrop_max)
  1952. rate = battle_config.item_drop_adddrop_max;
  1953. } else { //Relative drop, max/min limits are applied at drop time.
  1954. if (battle_config.item_rate_adddrop != 100)
  1955. rate = rate*battle_config.item_rate_adddrop/100;
  1956. if (rate > -1)
  1957. rate = -1;
  1958. }
  1959. //Find match entry, and adjust the rate only
  1960. for (i = 0; i < max; i++) {
  1961. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1962. continue;
  1963. if (drop[i].nameid == nameid &&
  1964. drop[i].group == group &&
  1965. drop[i].race == race &&
  1966. drop[i].class_ == class_
  1967. )
  1968. {
  1969. //Adjust the rate if it has same classification
  1970. if ((rate < 0 && drop[i].rate < 0) ||
  1971. (rate > 0 && drop[i].rate > 0))
  1972. {
  1973. drop[i].rate += rate;
  1974. return;
  1975. }
  1976. }
  1977. }
  1978. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1979. if (i >= max) {
  1980. 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);
  1981. return;
  1982. }
  1983. drop[i].nameid = nameid;
  1984. drop[i].group = group;
  1985. drop[i].race = race;
  1986. drop[i].class_ = class_;
  1987. drop[i].rate = rate;
  1988. }
  1989. 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)
  1990. {
  1991. int i;
  1992. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1993. if( i == max )
  1994. {
  1995. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1996. return false;
  1997. }
  1998. if( !onskill )
  1999. {
  2000. if( !(flag&BF_RANGEMASK) )
  2001. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  2002. if( !(flag&BF_WEAPONMASK) )
  2003. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  2004. if( !(flag&BF_SKILLMASK) )
  2005. {
  2006. if( flag&(BF_MAGIC|BF_MISC) )
  2007. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  2008. if( flag&BF_WEAPON )
  2009. flag|=BF_NORMAL|BF_SKILL;
  2010. }
  2011. }
  2012. bonus[i].rate = rate;
  2013. bonus[i].duration = dur;
  2014. bonus[i].active = INVALID_TIMER;
  2015. bonus[i].atk_type = flag;
  2016. bonus[i].pos = pos;
  2017. bonus[i].bonus_script = aStrdup(script);
  2018. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  2019. return true;
  2020. }
  2021. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  2022. {
  2023. int i;
  2024. if (!sd)
  2025. return;
  2026. for( i = 0; i < max; i++ )
  2027. {
  2028. if( autobonus[i].active != INVALID_TIMER )
  2029. {
  2030. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  2031. {
  2032. if( autobonus[i].bonus_script )
  2033. {
  2034. int j;
  2035. unsigned int equip_pos_idx = 0;
  2036. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2037. for(j = 0; j < EQI_MAX; j++) {
  2038. if(sd->equip_index[j] >= 0)
  2039. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2040. }
  2041. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  2042. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  2043. }
  2044. continue;
  2045. }
  2046. else
  2047. { // Logout / Unequipped an item with an activated bonus
  2048. delete_timer(autobonus[i].active,pc_endautobonus);
  2049. autobonus[i].active = INVALID_TIMER;
  2050. }
  2051. }
  2052. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  2053. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  2054. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  2055. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  2056. autobonus[i].active = INVALID_TIMER;
  2057. }
  2058. }
  2059. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  2060. {
  2061. if (!sd || !autobonus)
  2062. return;
  2063. if (autobonus->active != INVALID_TIMER)
  2064. delete_timer(autobonus->active, pc_endautobonus);
  2065. if( autobonus->other_script )
  2066. {
  2067. int j;
  2068. unsigned int equip_pos_idx = 0;
  2069. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2070. for(j = 0; j < EQI_MAX; j++) {
  2071. if(sd->equip_index[j] >= 0)
  2072. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2073. }
  2074. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2075. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2076. }
  2077. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  2078. sd->state.autobonus |= autobonus->pos;
  2079. status_calc_pc(sd,SCO_FORCE);
  2080. }
  2081. TIMER_FUNC(pc_endautobonus){
  2082. struct map_session_data *sd = map_id2sd(id);
  2083. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2084. nullpo_ret(sd);
  2085. nullpo_ret(autobonus);
  2086. autobonus->active = INVALID_TIMER;
  2087. sd->state.autobonus &= ~autobonus->pos;
  2088. status_calc_pc(sd,SCO_FORCE);
  2089. return 0;
  2090. }
  2091. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2092. {
  2093. uint8 i;
  2094. struct weapon_data* wd;
  2095. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2096. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2097. if (i == MAX_PC_BONUS)
  2098. {
  2099. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2100. return;
  2101. }
  2102. if (!(flag&BF_RANGEMASK))
  2103. flag |= BF_SHORT|BF_LONG;
  2104. if (!(flag&BF_WEAPONMASK))
  2105. flag |= BF_WEAPON;
  2106. if (!(flag&BF_SKILLMASK))
  2107. {
  2108. if (flag&(BF_MAGIC|BF_MISC))
  2109. flag |= BF_SKILL;
  2110. if (flag&BF_WEAPON)
  2111. flag |= BF_NORMAL|BF_SKILL;
  2112. }
  2113. wd->addele2[i].ele = ele;
  2114. wd->addele2[i].rate = rate;
  2115. wd->addele2[i].flag = flag;
  2116. }
  2117. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2118. {
  2119. uint8 i;
  2120. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2121. if (i == MAX_PC_BONUS)
  2122. {
  2123. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2124. return;
  2125. }
  2126. if (!(flag&BF_RANGEMASK))
  2127. flag |= BF_SHORT|BF_LONG;
  2128. if (!(flag&BF_WEAPONMASK))
  2129. flag |= BF_WEAPON;
  2130. if (!(flag&BF_SKILLMASK))
  2131. {
  2132. if (flag&(BF_MAGIC|BF_MISC))
  2133. flag |= BF_SKILL;
  2134. if (flag&BF_WEAPON)
  2135. flag |= BF_NORMAL|BF_SKILL;
  2136. }
  2137. sd->subele2[i].ele = ele;
  2138. sd->subele2[i].rate = rate;
  2139. sd->subele2[i].flag = flag;
  2140. }
  2141. /** Add item group heal rate bonus to player
  2142. * @param sd Player
  2143. * @param group_id Item Group ID
  2144. * @param rate
  2145. * @author Cydh
  2146. */
  2147. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2148. struct s_pc_itemgrouphealrate *entry;
  2149. uint8 i;
  2150. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2151. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2152. break;
  2153. }
  2154. if (i != sd->itemgrouphealrate_count) {
  2155. sd->itemgrouphealrate[i]->rate += rate;
  2156. return;
  2157. }
  2158. if (i >= UINT8_MAX) {
  2159. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2160. return;
  2161. }
  2162. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2163. entry->group_id = group_id;
  2164. entry->rate = rate;
  2165. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2166. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2167. }
  2168. /** Clear item group heal rate from player
  2169. * @param sd Player
  2170. * @author Cydh
  2171. */
  2172. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2173. if (!sd || !sd->itemgrouphealrate_count)
  2174. return;
  2175. else {
  2176. uint8 i;
  2177. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2178. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2179. sd->itemgrouphealrate_count = 0;
  2180. aFree(sd->itemgrouphealrate);
  2181. sd->itemgrouphealrate = NULL;
  2182. }
  2183. }
  2184. /*==========================================
  2185. * Add a bonus(type) to player sd
  2186. * format: bonus bBonusName,val;
  2187. * @param sd
  2188. * @param type Bonus type used by bBonusName
  2189. * @param val Value that usually for rate or fixed value
  2190. *------------------------------------------*/
  2191. void pc_bonus(struct map_session_data *sd,int type,int val)
  2192. {
  2193. struct status_data *status;
  2194. int bonus;
  2195. nullpo_retv(sd);
  2196. status = &sd->base_status;
  2197. switch(type){
  2198. case SP_STR:
  2199. case SP_AGI:
  2200. case SP_VIT:
  2201. case SP_INT:
  2202. case SP_DEX:
  2203. case SP_LUK:
  2204. if(sd->state.lr_flag != 2)
  2205. sd->param_bonus[type-SP_STR]+=val;
  2206. break;
  2207. case SP_ATK1:
  2208. if(!sd->state.lr_flag) {
  2209. bonus = status->rhw.atk + val;
  2210. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2211. }
  2212. else if(sd->state.lr_flag == 1) {
  2213. bonus = status->lhw.atk + val;
  2214. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2215. }
  2216. break;
  2217. case SP_ATK2:
  2218. if(!sd->state.lr_flag) {
  2219. bonus = status->rhw.atk2 + val;
  2220. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2221. }
  2222. else if(sd->state.lr_flag == 1) {
  2223. bonus = status->lhw.atk2 + val;
  2224. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2225. }
  2226. break;
  2227. case SP_BASE_ATK:
  2228. if(sd->state.lr_flag != 2) {
  2229. #ifdef RENEWAL
  2230. bonus = sd->bonus.eatk + val;
  2231. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2232. #else
  2233. bonus = status->batk + val;
  2234. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2235. #endif
  2236. }
  2237. break;
  2238. case SP_DEF1:
  2239. if(sd->state.lr_flag != 2) {
  2240. bonus = status->def + val;
  2241. #ifdef RENEWAL
  2242. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2243. #else
  2244. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2245. #endif
  2246. }
  2247. break;
  2248. case SP_DEF2:
  2249. if(sd->state.lr_flag != 2) {
  2250. bonus = status->def2 + val;
  2251. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2252. }
  2253. break;
  2254. case SP_MDEF1:
  2255. if(sd->state.lr_flag != 2) {
  2256. bonus = status->mdef + val;
  2257. #ifdef RENEWAL
  2258. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2259. #else
  2260. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2261. #endif
  2262. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2263. sd->bonus.shieldmdef += bonus;
  2264. }
  2265. }
  2266. break;
  2267. case SP_MDEF2:
  2268. if(sd->state.lr_flag != 2) {
  2269. bonus = status->mdef2 + val;
  2270. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2271. }
  2272. break;
  2273. case SP_HIT:
  2274. if(sd->state.lr_flag != 2) {
  2275. bonus = status->hit + val;
  2276. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2277. } else
  2278. sd->bonus.arrow_hit+=val;
  2279. break;
  2280. case SP_FLEE1:
  2281. if(sd->state.lr_flag != 2) {
  2282. bonus = status->flee + val;
  2283. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2284. }
  2285. break;
  2286. case SP_FLEE2:
  2287. if(sd->state.lr_flag != 2) {
  2288. bonus = status->flee2 + val*10;
  2289. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2290. }
  2291. break;
  2292. case SP_CRITICAL:
  2293. if(sd->state.lr_flag != 2) {
  2294. bonus = status->cri + val*10;
  2295. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2296. } else
  2297. sd->bonus.arrow_cri += val*10;
  2298. break;
  2299. case SP_ATKELE:
  2300. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2301. switch (sd->state.lr_flag)
  2302. {
  2303. case 2:
  2304. switch (sd->status.weapon) {
  2305. case W_BOW:
  2306. case W_REVOLVER:
  2307. case W_RIFLE:
  2308. case W_GATLING:
  2309. case W_SHOTGUN:
  2310. case W_GRENADE:
  2311. //Become weapon element.
  2312. status->rhw.ele=val;
  2313. break;
  2314. default: //Become arrow element.
  2315. sd->bonus.arrow_ele=val;
  2316. break;
  2317. }
  2318. break;
  2319. case 1:
  2320. status->lhw.ele=val;
  2321. break;
  2322. default:
  2323. status->rhw.ele=val;
  2324. break;
  2325. }
  2326. break;
  2327. case SP_DEFELE:
  2328. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2329. if(sd->state.lr_flag != 2)
  2330. status->def_ele=val;
  2331. break;
  2332. case SP_MAXHP:
  2333. if(sd->state.lr_flag == 2)
  2334. break;
  2335. sd->bonus.hp += val;
  2336. break;
  2337. case SP_MAXSP:
  2338. if(sd->state.lr_flag == 2)
  2339. break;
  2340. sd->bonus.sp += val;
  2341. break;
  2342. case SP_MAXHPRATE:
  2343. if(sd->state.lr_flag != 2)
  2344. sd->hprate+=val;
  2345. break;
  2346. case SP_MAXSPRATE:
  2347. if(sd->state.lr_flag != 2)
  2348. sd->sprate+=val;
  2349. break;
  2350. case SP_SPRATE:
  2351. if(sd->state.lr_flag != 2)
  2352. sd->dsprate+=val;
  2353. break;
  2354. case SP_ATTACKRANGE:
  2355. switch (sd->state.lr_flag) {
  2356. case 2:
  2357. switch (sd->status.weapon) {
  2358. case W_BOW:
  2359. case W_REVOLVER:
  2360. case W_RIFLE:
  2361. case W_GATLING:
  2362. case W_SHOTGUN:
  2363. case W_GRENADE:
  2364. status->rhw.range += val;
  2365. }
  2366. break;
  2367. case 1:
  2368. status->lhw.range += val;
  2369. break;
  2370. default:
  2371. status->rhw.range += val;
  2372. break;
  2373. }
  2374. break;
  2375. case SP_SPEED_RATE: //Non stackable increase
  2376. if(sd->state.lr_flag != 2)
  2377. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2378. break;
  2379. case SP_SPEED_ADDRATE: //Stackable increase
  2380. if(sd->state.lr_flag != 2)
  2381. sd->bonus.speed_add_rate -= val;
  2382. break;
  2383. case SP_ASPD: //Raw increase
  2384. if(sd->state.lr_flag != 2)
  2385. sd->bonus.aspd_add -= 10*val;
  2386. break;
  2387. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2388. if(sd->state.lr_flag != 2)
  2389. #ifndef RENEWAL_ASPD
  2390. status->aspd_rate -= 10*val;
  2391. #else
  2392. status->aspd_rate2 += val;
  2393. #endif
  2394. break;
  2395. case SP_HP_RECOV_RATE:
  2396. if(sd->state.lr_flag != 2)
  2397. sd->hprecov_rate += val;
  2398. break;
  2399. case SP_SP_RECOV_RATE:
  2400. if(sd->state.lr_flag != 2)
  2401. sd->sprecov_rate += val;
  2402. break;
  2403. case SP_CRITICAL_DEF:
  2404. if(sd->state.lr_flag != 2)
  2405. sd->bonus.critical_def += val;
  2406. break;
  2407. case SP_NEAR_ATK_DEF:
  2408. if(sd->state.lr_flag != 2)
  2409. sd->bonus.near_attack_def_rate += val;
  2410. break;
  2411. case SP_LONG_ATK_DEF:
  2412. if(sd->state.lr_flag != 2)
  2413. sd->bonus.long_attack_def_rate += val;
  2414. break;
  2415. case SP_DOUBLE_RATE:
  2416. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2417. sd->bonus.double_rate = val;
  2418. break;
  2419. case SP_DOUBLE_ADD_RATE:
  2420. if(sd->state.lr_flag == 0)
  2421. sd->bonus.double_add_rate += val;
  2422. break;
  2423. case SP_MATK_RATE:
  2424. if(sd->state.lr_flag != 2)
  2425. sd->matk_rate += val;
  2426. break;
  2427. case SP_IGNORE_DEF_ELE:
  2428. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2429. if(!sd->state.lr_flag)
  2430. sd->right_weapon.ignore_def_ele |= 1<<val;
  2431. else if(sd->state.lr_flag == 1)
  2432. sd->left_weapon.ignore_def_ele |= 1<<val;
  2433. break;
  2434. case SP_IGNORE_DEF_RACE:
  2435. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2436. if(!sd->state.lr_flag)
  2437. sd->right_weapon.ignore_def_race |= 1<<val;
  2438. else if(sd->state.lr_flag == 1)
  2439. sd->left_weapon.ignore_def_race |= 1<<val;
  2440. break;
  2441. case SP_IGNORE_DEF_CLASS:
  2442. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2443. if(!sd->state.lr_flag)
  2444. sd->right_weapon.ignore_def_class |= 1<<val;
  2445. else if(sd->state.lr_flag == 1)
  2446. sd->left_weapon.ignore_def_class |= 1<<val;
  2447. break;
  2448. case SP_ATK_RATE:
  2449. if(sd->state.lr_flag != 2)
  2450. sd->bonus.atk_rate += val;
  2451. break;
  2452. case SP_MAGIC_ATK_DEF:
  2453. if(sd->state.lr_flag != 2)
  2454. sd->bonus.magic_def_rate += val;
  2455. break;
  2456. case SP_MISC_ATK_DEF:
  2457. if(sd->state.lr_flag != 2)
  2458. sd->bonus.misc_def_rate += val;
  2459. break;
  2460. case SP_IGNORE_MDEF_ELE:
  2461. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2462. if(sd->state.lr_flag != 2)
  2463. sd->bonus.ignore_mdef_ele |= 1<<val;
  2464. break;
  2465. case SP_IGNORE_MDEF_RACE:
  2466. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2467. if(sd->state.lr_flag != 2)
  2468. sd->bonus.ignore_mdef_race |= 1<<val;
  2469. break;
  2470. case SP_PERFECT_HIT_RATE:
  2471. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2472. sd->bonus.perfect_hit = val;
  2473. break;
  2474. case SP_PERFECT_HIT_ADD_RATE:
  2475. if(sd->state.lr_flag != 2)
  2476. sd->bonus.perfect_hit_add += val;
  2477. break;
  2478. case SP_CRITICAL_RATE:
  2479. if(sd->state.lr_flag != 2)
  2480. sd->critical_rate+=val;
  2481. break;
  2482. case SP_DEF_RATIO_ATK_ELE:
  2483. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2484. if(!sd->state.lr_flag)
  2485. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2486. else if(sd->state.lr_flag == 1)
  2487. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2488. break;
  2489. case SP_DEF_RATIO_ATK_RACE:
  2490. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2491. if(!sd->state.lr_flag)
  2492. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2493. else if(sd->state.lr_flag == 1)
  2494. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2495. break;
  2496. case SP_DEF_RATIO_ATK_CLASS:
  2497. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2498. if(!sd->state.lr_flag)
  2499. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2500. else if(sd->state.lr_flag == 1)
  2501. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2502. break;
  2503. case SP_HIT_RATE:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->hit_rate += val;
  2506. break;
  2507. case SP_FLEE_RATE:
  2508. if(sd->state.lr_flag != 2)
  2509. sd->flee_rate += val;
  2510. break;
  2511. case SP_FLEE2_RATE:
  2512. if(sd->state.lr_flag != 2)
  2513. sd->flee2_rate += val;
  2514. break;
  2515. case SP_DEF_RATE:
  2516. if(sd->state.lr_flag != 2)
  2517. sd->def_rate += val;
  2518. break;
  2519. case SP_DEF2_RATE:
  2520. if(sd->state.lr_flag != 2)
  2521. sd->def2_rate += val;
  2522. break;
  2523. case SP_MDEF_RATE:
  2524. if(sd->state.lr_flag != 2)
  2525. sd->mdef_rate += val;
  2526. break;
  2527. case SP_MDEF2_RATE:
  2528. if(sd->state.lr_flag != 2)
  2529. sd->mdef2_rate += val;
  2530. break;
  2531. case SP_RESTART_FULL_RECOVER:
  2532. if(sd->state.lr_flag != 2)
  2533. sd->special_state.restart_full_recover = 1;
  2534. break;
  2535. case SP_NO_CASTCANCEL:
  2536. if(sd->state.lr_flag != 2)
  2537. sd->special_state.no_castcancel = 1;
  2538. break;
  2539. case SP_NO_CASTCANCEL2:
  2540. if(sd->state.lr_flag != 2)
  2541. sd->special_state.no_castcancel2 = 1;
  2542. break;
  2543. case SP_NO_SIZEFIX:
  2544. if(sd->state.lr_flag != 2)
  2545. sd->special_state.no_sizefix = 1;
  2546. break;
  2547. case SP_NO_MAGIC_DAMAGE:
  2548. if(sd->state.lr_flag == 2)
  2549. break;
  2550. val+= sd->special_state.no_magic_damage;
  2551. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2552. break;
  2553. case SP_NO_WEAPON_DAMAGE:
  2554. if(sd->state.lr_flag == 2)
  2555. break;
  2556. val+= sd->special_state.no_weapon_damage;
  2557. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2558. break;
  2559. case SP_NO_MISC_DAMAGE:
  2560. if(sd->state.lr_flag == 2)
  2561. break;
  2562. val+= sd->special_state.no_misc_damage;
  2563. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2564. break;
  2565. case SP_NO_GEMSTONE:
  2566. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2567. sd->special_state.no_gemstone = 1;
  2568. break;
  2569. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2570. if(sd->state.lr_flag != 2) {
  2571. sd->special_state.intravision = 1;
  2572. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2573. }
  2574. break;
  2575. case SP_NO_KNOCKBACK:
  2576. if(sd->state.lr_flag != 2)
  2577. sd->special_state.no_knockback = 1;
  2578. break;
  2579. case SP_SPLASH_RANGE:
  2580. if(sd->bonus.splash_range < val)
  2581. sd->bonus.splash_range = val;
  2582. break;
  2583. case SP_SPLASH_ADD_RANGE:
  2584. sd->bonus.splash_add_range += val;
  2585. break;
  2586. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2587. if(sd->state.lr_flag != 2)
  2588. sd->bonus.short_weapon_damage_return += val;
  2589. break;
  2590. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2591. if(sd->state.lr_flag != 2)
  2592. sd->bonus.long_weapon_damage_return += val;
  2593. break;
  2594. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2595. if(sd->state.lr_flag != 2)
  2596. sd->bonus.magic_damage_return += val;
  2597. break;
  2598. case SP_ALL_STATS: // [Valaris]
  2599. if(sd->state.lr_flag!=2) {
  2600. sd->param_bonus[SP_STR-SP_STR]+=val;
  2601. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2602. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2603. sd->param_bonus[SP_INT-SP_STR]+=val;
  2604. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2605. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2606. }
  2607. break;
  2608. case SP_AGI_VIT: // [Valaris]
  2609. if(sd->state.lr_flag!=2) {
  2610. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2611. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2612. }
  2613. break;
  2614. case SP_AGI_DEX_STR: // [Valaris]
  2615. if(sd->state.lr_flag!=2) {
  2616. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2617. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2618. sd->param_bonus[SP_STR-SP_STR]+=val;
  2619. }
  2620. break;
  2621. case SP_PERFECT_HIDE: // [Valaris]
  2622. if(sd->state.lr_flag!=2)
  2623. sd->special_state.perfect_hiding=1;
  2624. break;
  2625. case SP_UNBREAKABLE:
  2626. if(sd->state.lr_flag!=2)
  2627. sd->bonus.unbreakable += val;
  2628. break;
  2629. case SP_UNBREAKABLE_WEAPON:
  2630. if(sd->state.lr_flag != 2)
  2631. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2632. break;
  2633. case SP_UNBREAKABLE_ARMOR:
  2634. if(sd->state.lr_flag != 2)
  2635. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2636. break;
  2637. case SP_UNBREAKABLE_HELM:
  2638. if(sd->state.lr_flag != 2)
  2639. sd->bonus.unbreakable_equip |= EQP_HELM;
  2640. break;
  2641. case SP_UNBREAKABLE_SHIELD:
  2642. if(sd->state.lr_flag != 2)
  2643. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2644. break;
  2645. case SP_UNBREAKABLE_GARMENT:
  2646. if(sd->state.lr_flag != 2)
  2647. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2648. break;
  2649. case SP_UNBREAKABLE_SHOES:
  2650. if(sd->state.lr_flag != 2)
  2651. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2652. break;
  2653. case SP_CLASSCHANGE: // [Valaris]
  2654. if(sd->state.lr_flag !=2)
  2655. sd->bonus.classchange=val;
  2656. break;
  2657. case SP_LONG_ATK_RATE:
  2658. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2659. sd->bonus.long_attack_atk_rate+=val;
  2660. break;
  2661. case SP_BREAK_WEAPON_RATE:
  2662. if(sd->state.lr_flag != 2)
  2663. sd->bonus.break_weapon_rate+=val;
  2664. break;
  2665. case SP_BREAK_ARMOR_RATE:
  2666. if(sd->state.lr_flag != 2)
  2667. sd->bonus.break_armor_rate+=val;
  2668. break;
  2669. case SP_ADD_STEAL_RATE:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.add_steal_rate+=val;
  2672. break;
  2673. case SP_DELAYRATE:
  2674. if(sd->state.lr_flag != 2)
  2675. sd->delayrate+=val;
  2676. break;
  2677. case SP_CRIT_ATK_RATE:
  2678. if(sd->state.lr_flag != 2)
  2679. sd->bonus.crit_atk_rate += val;
  2680. break;
  2681. case SP_NO_REGEN:
  2682. if(sd->state.lr_flag != 2)
  2683. sd->regen.state.block|=val;
  2684. break;
  2685. case SP_UNSTRIPABLE_WEAPON:
  2686. if(sd->state.lr_flag != 2)
  2687. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2688. break;
  2689. case SP_UNSTRIPABLE:
  2690. case SP_UNSTRIPABLE_ARMOR:
  2691. if(sd->state.lr_flag != 2)
  2692. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2693. break;
  2694. case SP_UNSTRIPABLE_HELM:
  2695. if(sd->state.lr_flag != 2)
  2696. sd->bonus.unstripable_equip |= EQP_HELM;
  2697. break;
  2698. case SP_UNSTRIPABLE_SHIELD:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2701. break;
  2702. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2703. if(!sd->state.lr_flag) {
  2704. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2705. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2706. } else if(sd->state.lr_flag == 1) {
  2707. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2708. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2709. }
  2710. break;
  2711. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2712. if(!sd->state.lr_flag) {
  2713. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2714. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2715. } else if(sd->state.lr_flag == 1) {
  2716. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2717. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2718. }
  2719. break;
  2720. case SP_SP_GAIN_VALUE:
  2721. if(!sd->state.lr_flag)
  2722. sd->bonus.sp_gain_value += val;
  2723. break;
  2724. case SP_HP_GAIN_VALUE:
  2725. if(!sd->state.lr_flag)
  2726. sd->bonus.hp_gain_value += val;
  2727. break;
  2728. case SP_MAGIC_SP_GAIN_VALUE:
  2729. if(!sd->state.lr_flag)
  2730. sd->bonus.magic_sp_gain_value += val;
  2731. break;
  2732. case SP_MAGIC_HP_GAIN_VALUE:
  2733. if(!sd->state.lr_flag)
  2734. sd->bonus.magic_hp_gain_value += val;
  2735. break;
  2736. case SP_ADD_HEAL_RATE:
  2737. if(sd->state.lr_flag != 2)
  2738. sd->bonus.add_heal_rate += val;
  2739. break;
  2740. case SP_ADD_HEAL2_RATE:
  2741. if(sd->state.lr_flag != 2)
  2742. sd->bonus.add_heal2_rate += val;
  2743. break;
  2744. case SP_ADD_ITEM_HEAL_RATE:
  2745. if(sd->state.lr_flag != 2)
  2746. sd->bonus.itemhealrate2 += val;
  2747. break;
  2748. case SP_EMATK:
  2749. if(sd->state.lr_flag != 2)
  2750. sd->bonus.ematk += val;
  2751. break;
  2752. #ifdef RENEWAL_CAST
  2753. case SP_FIXCASTRATE:
  2754. if(sd->state.lr_flag != 2)
  2755. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2756. break;
  2757. case SP_ADD_FIXEDCAST:
  2758. if(sd->state.lr_flag != 2)
  2759. sd->bonus.add_fixcast += val;
  2760. break;
  2761. case SP_CASTRATE:
  2762. case SP_VARCASTRATE:
  2763. if(sd->state.lr_flag != 2)
  2764. sd->bonus.varcastrate -= val;
  2765. break;
  2766. case SP_ADD_VARIABLECAST:
  2767. if(sd->state.lr_flag != 2)
  2768. sd->bonus.add_varcast += val;
  2769. break;
  2770. #else
  2771. case SP_ADD_FIXEDCAST:
  2772. case SP_FIXCASTRATE:
  2773. case SP_ADD_VARIABLECAST:
  2774. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2775. break;
  2776. case SP_VARCASTRATE:
  2777. case SP_CASTRATE:
  2778. if(sd->state.lr_flag != 2)
  2779. sd->castrate += val;
  2780. break;
  2781. #endif
  2782. case SP_ADDMAXWEIGHT:
  2783. if (sd->state.lr_flag != 2)
  2784. sd->add_max_weight += val;
  2785. break;
  2786. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2787. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2788. break;
  2789. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2790. if (sd->state.lr_flag != 2)
  2791. sd->bonus.critical_rangeatk += val*10;
  2792. else
  2793. sd->bonus.arrow_cri += val*10;
  2794. break;
  2795. case SP_WEAPON_ATK_RATE:
  2796. if (sd->state.lr_flag != 2)
  2797. sd->bonus.weapon_atk_rate += val;
  2798. break;
  2799. case SP_WEAPON_MATK_RATE:
  2800. if (sd->state.lr_flag != 2)
  2801. sd->bonus.weapon_matk_rate += val;
  2802. break;
  2803. case SP_NO_MADO_FUEL:
  2804. if (sd->state.lr_flag != 2)
  2805. sd->special_state.no_mado_fuel = 1;
  2806. break;
  2807. default:
  2808. if (running_npc_stat_calc_event) {
  2809. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2810. }
  2811. else if (current_equip_combo_pos > 0) {
  2812. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  2813. }
  2814. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2815. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  2816. }
  2817. else {
  2818. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2819. }
  2820. break;
  2821. }
  2822. }
  2823. /*==========================================
  2824. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2825. * format: bonus2 bBonusName,type2,val;
  2826. * @param sd
  2827. * @param type Bonus type used by bBonusName
  2828. * @param type2
  2829. * @param val Value that usually for rate or fixed value
  2830. *------------------------------------------*/
  2831. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2832. {
  2833. int i;
  2834. nullpo_retv(sd);
  2835. switch(type){
  2836. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2837. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2838. if(!sd->state.lr_flag)
  2839. sd->right_weapon.addele[type2]+=val;
  2840. else if(sd->state.lr_flag == 1)
  2841. sd->left_weapon.addele[type2]+=val;
  2842. else if(sd->state.lr_flag == 2)
  2843. sd->arrow_addele[type2]+=val;
  2844. break;
  2845. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2846. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2847. if(!sd->state.lr_flag)
  2848. sd->right_weapon.addrace[type2]+=val;
  2849. else if(sd->state.lr_flag == 1)
  2850. sd->left_weapon.addrace[type2]+=val;
  2851. else if(sd->state.lr_flag == 2)
  2852. sd->arrow_addrace[type2]+=val;
  2853. break;
  2854. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2855. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2856. if(!sd->state.lr_flag)
  2857. sd->right_weapon.addclass[type2]+=val;
  2858. else if(sd->state.lr_flag == 1)
  2859. sd->left_weapon.addclass[type2]+=val;
  2860. else if(sd->state.lr_flag == 2)
  2861. sd->arrow_addclass[type2]+=val;
  2862. break;
  2863. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2864. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2865. if(!sd->state.lr_flag)
  2866. sd->right_weapon.addsize[type2]+=val;
  2867. else if(sd->state.lr_flag == 1)
  2868. sd->left_weapon.addsize[type2]+=val;
  2869. else if(sd->state.lr_flag == 2)
  2870. sd->arrow_addsize[type2]+=val;
  2871. break;
  2872. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2873. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2874. if(sd->state.lr_flag != 2)
  2875. sd->subele_script[type2] += val;
  2876. break;
  2877. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2878. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2879. if(sd->state.lr_flag != 2)
  2880. sd->subrace[type2]+=val;
  2881. break;
  2882. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2883. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2884. if(sd->state.lr_flag != 2)
  2885. sd->subclass[type2]+=val;
  2886. break;
  2887. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2888. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2889. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2890. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2891. break;
  2892. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2893. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2894. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2895. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2896. break;
  2897. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2898. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2899. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2900. break;
  2901. }
  2902. if(sd->state.lr_flag == 2)
  2903. break;
  2904. i = sd->reseff[type2]+val;
  2905. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2906. break;
  2907. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2908. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2909. if(sd->state.lr_flag != 2)
  2910. sd->magic_addele_script[type2] += val;
  2911. break;
  2912. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2913. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2914. if(sd->state.lr_flag != 2)
  2915. sd->magic_addrace[type2]+=val;
  2916. break;
  2917. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2918. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2919. if(sd->state.lr_flag != 2)
  2920. sd->magic_addclass[type2]+=val;
  2921. break;
  2922. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2923. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2924. if(sd->state.lr_flag != 2)
  2925. sd->magic_addsize[type2]+=val;
  2926. break;
  2927. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2928. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2929. if(sd->state.lr_flag != 2)
  2930. sd->magic_atk_ele[type2]+=val;
  2931. break;
  2932. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2933. switch (sd->state.lr_flag) {
  2934. case 0: //Right hand
  2935. 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);
  2936. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2937. {
  2938. 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));
  2939. break;
  2940. }
  2941. sd->right_weapon.add_dmg[i].class_ = type2;
  2942. sd->right_weapon.add_dmg[i].rate += val;
  2943. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2944. 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]));
  2945. break;
  2946. case 1: //Left hand
  2947. 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);
  2948. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2949. {
  2950. 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));
  2951. break;
  2952. }
  2953. sd->left_weapon.add_dmg[i].class_ = type2;
  2954. sd->left_weapon.add_dmg[i].rate += val;
  2955. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2956. 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]));
  2957. break;
  2958. }
  2959. break;
  2960. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2961. if(sd->state.lr_flag == 2)
  2962. break;
  2963. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2964. if (i == ARRAYLENGTH(sd->add_mdmg))
  2965. {
  2966. 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));
  2967. break;
  2968. }
  2969. sd->add_mdmg[i].class_ = type2;
  2970. sd->add_mdmg[i].rate += val;
  2971. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2972. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2973. break;
  2974. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2975. if(sd->state.lr_flag == 2)
  2976. break;
  2977. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2978. if (i == ARRAYLENGTH(sd->add_def))
  2979. {
  2980. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2981. break;
  2982. }
  2983. sd->add_def[i].class_ = type2;
  2984. sd->add_def[i].rate += val;
  2985. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2986. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2987. break;
  2988. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2989. if(sd->state.lr_flag == 2)
  2990. break;
  2991. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2992. if (i == ARRAYLENGTH(sd->add_mdef))
  2993. {
  2994. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2995. break;
  2996. }
  2997. sd->add_mdef[i].class_ = type2;
  2998. sd->add_mdef[i].rate += val;
  2999. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  3000. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  3001. break;
  3002. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3003. if(!sd->state.lr_flag) {
  3004. sd->right_weapon.hp_drain_rate.rate += type2;
  3005. sd->right_weapon.hp_drain_rate.per += val;
  3006. }
  3007. else if(sd->state.lr_flag == 1) {
  3008. sd->left_weapon.hp_drain_rate.rate += type2;
  3009. sd->left_weapon.hp_drain_rate.per += val;
  3010. }
  3011. break;
  3012. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3013. if(!sd->state.lr_flag) {
  3014. sd->right_weapon.sp_drain_rate.rate += type2;
  3015. sd->right_weapon.sp_drain_rate.per += val;
  3016. }
  3017. else if(sd->state.lr_flag == 1) {
  3018. sd->left_weapon.sp_drain_rate.rate += type2;
  3019. sd->left_weapon.sp_drain_rate.per += val;
  3020. }
  3021. break;
  3022. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3023. if(sd->state.lr_flag != 2) {
  3024. sd->bonus.sp_vanish_rate += type2;
  3025. sd->bonus.sp_vanish_per += val;
  3026. }
  3027. break;
  3028. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3029. if(sd->state.lr_flag != 2) {
  3030. sd->bonus.hp_vanish_rate += type2;
  3031. sd->bonus.hp_vanish_per += val;
  3032. }
  3033. break;
  3034. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3035. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3036. sd->bonus.get_zeny_rate = val;
  3037. sd->bonus.get_zeny_num = type2;
  3038. }
  3039. break;
  3040. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3041. if(sd->state.lr_flag != 2) {
  3042. sd->bonus.get_zeny_rate += val;
  3043. sd->bonus.get_zeny_num += type2;
  3044. }
  3045. break;
  3046. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3047. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3048. if(sd->state.lr_flag == 2)
  3049. break;
  3050. sd->weapon_coma_ele[type2] += val;
  3051. sd->special_state.bonus_coma = 1;
  3052. break;
  3053. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3054. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3055. if(sd->state.lr_flag == 2)
  3056. break;
  3057. sd->weapon_coma_race[type2] += val;
  3058. sd->special_state.bonus_coma = 1;
  3059. break;
  3060. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3061. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3062. if(sd->state.lr_flag == 2)
  3063. break;
  3064. sd->weapon_coma_class[type2] += val;
  3065. sd->special_state.bonus_coma = 1;
  3066. break;
  3067. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3068. if(sd->state.lr_flag != 2)
  3069. sd->weapon_atk[type2]+=val;
  3070. break;
  3071. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3072. if(sd->state.lr_flag != 2)
  3073. sd->weapon_damage_rate[type2]+=val;
  3074. break;
  3075. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3076. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3077. if(sd->state.lr_flag != 2)
  3078. sd->critaddrace[type2] += val*10;
  3079. break;
  3080. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3081. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3082. if(sd->state.lr_flag != 2)
  3083. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  3084. break;
  3085. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3086. if(sd->state.lr_flag == 2)
  3087. break;
  3088. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3089. if (i == ARRAYLENGTH(sd->skillatk))
  3090. { //Better mention this so the array length can be updated. [Skotlex]
  3091. 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);
  3092. break;
  3093. }
  3094. if (sd->skillatk[i].id == type2)
  3095. sd->skillatk[i].val += val;
  3096. else {
  3097. sd->skillatk[i].id = type2;
  3098. sd->skillatk[i].val = val;
  3099. }
  3100. break;
  3101. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3102. if(sd->state.lr_flag == 2)
  3103. break;
  3104. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3105. if (i == ARRAYLENGTH(sd->skillheal))
  3106. { // Better mention this so the array length can be updated. [Skotlex]
  3107. 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);
  3108. break;
  3109. }
  3110. if (sd->skillheal[i].id == type2)
  3111. sd->skillheal[i].val += val;
  3112. else {
  3113. sd->skillheal[i].id = type2;
  3114. sd->skillheal[i].val = val;
  3115. }
  3116. break;
  3117. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3118. if(sd->state.lr_flag == 2)
  3119. break;
  3120. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3121. if (i == ARRAYLENGTH(sd->skillheal2))
  3122. { // Better mention this so the array length can be updated. [Skotlex]
  3123. 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);
  3124. break;
  3125. }
  3126. if (sd->skillheal2[i].id == type2)
  3127. sd->skillheal2[i].val += val;
  3128. else {
  3129. sd->skillheal2[i].id = type2;
  3130. sd->skillheal2[i].val = val;
  3131. }
  3132. break;
  3133. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3134. if(sd->state.lr_flag == 2)
  3135. break;
  3136. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3137. if (i == ARRAYLENGTH(sd->skillblown))
  3138. { //Better mention this so the array length can be updated. [Skotlex]
  3139. 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);
  3140. break;
  3141. }
  3142. if(sd->skillblown[i].id == type2)
  3143. sd->skillblown[i].val += val;
  3144. else {
  3145. sd->skillblown[i].id = type2;
  3146. sd->skillblown[i].val = val;
  3147. }
  3148. break;
  3149. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3150. if(sd->state.lr_flag != 2) {
  3151. sd->hp_loss.value = type2;
  3152. sd->hp_loss.rate = val;
  3153. }
  3154. break;
  3155. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3156. if(sd->state.lr_flag != 2) {
  3157. sd->hp_regen.value = type2;
  3158. sd->hp_regen.rate = val;
  3159. }
  3160. break;
  3161. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3162. if (sd->state.lr_flag != 2) {
  3163. sd->percent_hp_regen.value = type2;
  3164. sd->percent_hp_regen.rate = val;
  3165. }
  3166. break;
  3167. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3168. if (sd->state.lr_flag != 2) {
  3169. sd->percent_sp_regen.value = type2;
  3170. sd->percent_sp_regen.rate = val;
  3171. }
  3172. break;
  3173. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3174. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3175. if(sd->state.lr_flag != 2)
  3176. sd->right_weapon.addrace2[type2] += val;
  3177. else
  3178. sd->left_weapon.addrace2[type2] += val;
  3179. break;
  3180. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3181. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3182. if(sd->state.lr_flag != 2)
  3183. sd->subsize[type2]+=val;
  3184. break;
  3185. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3186. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3187. if(sd->state.lr_flag != 2)
  3188. sd->subrace2[type2]+=val;
  3189. break;
  3190. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3191. if(sd->state.lr_flag == 2)
  3192. break;
  3193. if (!itemdb_exists(type2)) {
  3194. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3195. break;
  3196. }
  3197. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3198. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3199. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3200. break;
  3201. }
  3202. sd->itemhealrate[i].nameid = type2;
  3203. sd->itemhealrate[i].rate += val;
  3204. break;
  3205. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3206. if (sd->state.lr_flag == 2)
  3207. break;
  3208. if (!type2 || !itemdb_group_exists(type2)) {
  3209. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3210. break;
  3211. }
  3212. pc_itemgrouphealrate(sd, type2, val);
  3213. break;
  3214. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3215. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3216. if(sd->state.lr_flag != 2)
  3217. sd->expaddrace[type2]+=val;
  3218. break;
  3219. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3220. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3221. if(sd->state.lr_flag != 2)
  3222. sd->expaddclass[type2]+=val;
  3223. break;
  3224. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3225. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3226. if(sd->state.lr_flag != 2)
  3227. sd->sp_gain_race[type2]+=val;
  3228. break;
  3229. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3230. if (sd->state.lr_flag != 2)
  3231. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3232. break;
  3233. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3234. if (sd->state.lr_flag != 2)
  3235. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3236. break;
  3237. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3238. if(sd->state.lr_flag != 2) {
  3239. sd->sp_loss.value = type2;
  3240. sd->sp_loss.rate = val;
  3241. }
  3242. break;
  3243. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3244. if(sd->state.lr_flag != 2) {
  3245. sd->sp_regen.value = type2;
  3246. sd->sp_regen.rate = val;
  3247. }
  3248. break;
  3249. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3250. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3251. if(!sd->state.lr_flag) {
  3252. sd->right_weapon.hp_drain_race[type2] += val;
  3253. }
  3254. else if(sd->state.lr_flag == 1) {
  3255. sd->left_weapon.hp_drain_race[type2] += val;
  3256. }
  3257. break;
  3258. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3259. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3260. if(!sd->state.lr_flag) {
  3261. sd->right_weapon.sp_drain_race[type2] += val;
  3262. }
  3263. else if(sd->state.lr_flag == 1) {
  3264. sd->left_weapon.sp_drain_race[type2] += val;
  3265. }
  3266. break;
  3267. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3268. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3269. if(!sd->state.lr_flag) {
  3270. sd->right_weapon.hp_drain_class[type2] += val;
  3271. }
  3272. else if(sd->state.lr_flag == 1) {
  3273. sd->left_weapon.hp_drain_class[type2] += val;
  3274. }
  3275. break;
  3276. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3277. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3278. if(!sd->state.lr_flag) {
  3279. sd->right_weapon.sp_drain_class[type2] += val;
  3280. }
  3281. else if(sd->state.lr_flag == 1) {
  3282. sd->left_weapon.sp_drain_class[type2] += val;
  3283. }
  3284. break;
  3285. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3286. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3287. if(sd->state.lr_flag != 2)
  3288. sd->ignore_mdef_by_race[type2] += val;
  3289. break;
  3290. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3291. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3292. if(sd->state.lr_flag != 2)
  3293. sd->ignore_mdef_by_class[type2] += val;
  3294. break;
  3295. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3296. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3297. if(sd->state.lr_flag != 2)
  3298. sd->ignore_def_by_race[type2] += val;
  3299. break;
  3300. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3301. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3302. if (sd->state.lr_flag != 2)
  3303. sd->ignore_def_by_class[type2] += val;
  3304. break;
  3305. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3306. if(sd->state.lr_flag == 2)
  3307. break;
  3308. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3309. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3310. 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);
  3311. break;
  3312. }
  3313. if (sd->skillusesprate[i].id == type2)
  3314. sd->skillusesprate[i].val += val;
  3315. else {
  3316. sd->skillusesprate[i].id = type2;
  3317. sd->skillusesprate[i].val = val;
  3318. }
  3319. break;
  3320. case SP_SKILL_DELAY:
  3321. if(sd->state.lr_flag == 2)
  3322. break;
  3323. ARR_FIND(0, ARRAYLENGTH(sd->skilldelay), i, sd->skilldelay[i].id == 0 || sd->skilldelay[i].id == type2);
  3324. if (i == ARRAYLENGTH(sd->skilldelay)) {
  3325. ShowError("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skilldelay), type2, val);
  3326. break;
  3327. }
  3328. if (sd->skilldelay[i].id == type2)
  3329. sd->skilldelay[i].val += val;
  3330. else {
  3331. sd->skilldelay[i].id = type2;
  3332. sd->skilldelay[i].val = val;
  3333. }
  3334. break;
  3335. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3336. if(sd->state.lr_flag == 2)
  3337. break;
  3338. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3339. if (i == ARRAYLENGTH(sd->skillcooldown))
  3340. {
  3341. 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);
  3342. break;
  3343. }
  3344. if (sd->skillcooldown[i].id == type2)
  3345. sd->skillcooldown[i].val += val;
  3346. else {
  3347. sd->skillcooldown[i].id = type2;
  3348. sd->skillcooldown[i].val = val;
  3349. }
  3350. break;
  3351. #ifdef RENEWAL_CAST
  3352. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3353. if(sd->state.lr_flag == 2)
  3354. break;
  3355. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3356. if (i == ARRAYLENGTH(sd->skillfixcast))
  3357. {
  3358. 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);
  3359. break;
  3360. }
  3361. if (sd->skillfixcast[i].id == type2)
  3362. sd->skillfixcast[i].val += val;
  3363. else {
  3364. sd->skillfixcast[i].id = type2;
  3365. sd->skillfixcast[i].val = val;
  3366. }
  3367. break;
  3368. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3369. if(sd->state.lr_flag == 2)
  3370. break;
  3371. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3372. if (i == ARRAYLENGTH(sd->skillvarcast))
  3373. {
  3374. 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);
  3375. break;
  3376. }
  3377. if (sd->skillvarcast[i].id == type2)
  3378. sd->skillvarcast[i].val += val;
  3379. else {
  3380. sd->skillvarcast[i].id = type2;
  3381. sd->skillvarcast[i].val = val;
  3382. }
  3383. break;
  3384. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3385. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3386. if(sd->state.lr_flag == 2)
  3387. break;
  3388. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3389. if (i == ARRAYLENGTH(sd->skillcastrate))
  3390. {
  3391. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3392. break;
  3393. }
  3394. if(sd->skillcastrate[i].id == type2)
  3395. sd->skillcastrate[i].val -= val;
  3396. else {
  3397. sd->skillcastrate[i].id = type2;
  3398. sd->skillcastrate[i].val -= val;
  3399. }
  3400. break;
  3401. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3402. if(sd->state.lr_flag == 2)
  3403. break;
  3404. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3405. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3406. {
  3407. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3408. break;
  3409. }
  3410. if(sd->skillfixcastrate[i].id == type2)
  3411. sd->skillfixcastrate[i].val -= val;
  3412. else {
  3413. sd->skillfixcastrate[i].id = type2;
  3414. sd->skillfixcastrate[i].val -= val;
  3415. }
  3416. break;
  3417. #else
  3418. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3419. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3420. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3421. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3422. break;
  3423. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3424. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3425. if(sd->state.lr_flag == 2)
  3426. break;
  3427. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3428. if (i == ARRAYLENGTH(sd->skillcastrate))
  3429. { //Better mention this so the array length can be updated. [Skotlex]
  3430. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3431. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3432. break;
  3433. }
  3434. if(sd->skillcastrate[i].id == type2)
  3435. sd->skillcastrate[i].val += val;
  3436. else {
  3437. sd->skillcastrate[i].id = type2;
  3438. sd->skillcastrate[i].val = val;
  3439. }
  3440. break;
  3441. #endif
  3442. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3443. if(sd->state.lr_flag == 2)
  3444. break;
  3445. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3446. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3447. 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);
  3448. break;
  3449. }
  3450. if (sd->skillusesp[i].id == type2)
  3451. sd->skillusesp[i].val += val;
  3452. else {
  3453. sd->skillusesp[i].id = type2;
  3454. sd->skillusesp[i].val = val;
  3455. }
  3456. break;
  3457. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3458. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3459. if (i == ARRAYLENGTH(sd->subskill)) {
  3460. 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);
  3461. break;
  3462. }
  3463. if (sd->subskill[i].id == type2)
  3464. sd->subskill[i].val += val;
  3465. else {
  3466. sd->subskill[i].id = type2;
  3467. sd->subskill[i].val = val;
  3468. }
  3469. break;
  3470. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3471. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3472. sd->subdefele[type2] += val;
  3473. break;
  3474. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3475. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3476. sd->coma_class[type2] += val;
  3477. sd->special_state.bonus_coma = 1;
  3478. break;
  3479. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3480. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3481. sd->coma_race[type2] += val;
  3482. sd->special_state.bonus_coma = 1;
  3483. break;
  3484. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3485. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3486. if(sd->state.lr_flag != 2)
  3487. sd->magic_addrace2[type2] += val;
  3488. break;
  3489. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3490. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3491. if (sd->state.lr_flag != 2)
  3492. sd->ignore_mdef_by_race2[type2] += val;
  3493. break;
  3494. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3495. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3496. if (sd->state.lr_flag != 2)
  3497. sd->dropaddrace[type2] += val;
  3498. break;
  3499. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3500. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3501. if (sd->state.lr_flag != 2)
  3502. sd->dropaddclass[type2] += val;
  3503. break;
  3504. default:
  3505. if (running_npc_stat_calc_event) {
  3506. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3507. }
  3508. else if (current_equip_combo_pos > 0) {
  3509. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3510. }
  3511. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3512. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3513. }
  3514. else {
  3515. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3516. }
  3517. break;
  3518. }
  3519. }
  3520. /**
  3521. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3522. * @param sd
  3523. * @param type Bonus type used by bBonusName
  3524. * @param type2
  3525. * @param val Value that usually for rate or fixed value
  3526. */
  3527. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3528. {
  3529. nullpo_retv(sd);
  3530. switch(type){
  3531. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3532. if(sd->state.lr_flag != 2)
  3533. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3534. break;
  3535. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3536. if(sd->state.lr_flag != 2)
  3537. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3538. break;
  3539. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3540. if(sd->state.lr_flag != 2)
  3541. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3542. break;
  3543. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3544. if(sd->state.lr_flag != 2)
  3545. {
  3546. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3547. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3548. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3549. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3550. }
  3551. break;
  3552. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3553. if(sd->state.lr_flag != 2)
  3554. {
  3555. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3556. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3557. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3558. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3559. }
  3560. break;
  3561. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3562. if (sd->state.lr_flag != 2)
  3563. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3564. break;
  3565. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3566. if (sd->state.lr_flag != 2)
  3567. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3568. break;
  3569. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3570. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3571. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3572. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3573. break;
  3574. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3575. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3576. if(sd->state.lr_flag != 2)
  3577. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3578. break;
  3579. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3580. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3581. if( sd->state.lr_flag != 2 )
  3582. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3583. break;
  3584. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3585. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3586. if (sd->state.lr_flag != 2)
  3587. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3588. break;
  3589. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3590. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3591. if (sd->state.lr_flag != 2)
  3592. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3593. break;
  3594. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3595. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3596. if(sd->state.lr_flag != 2) {
  3597. sd->sp_vanish_race[type2].rate += type3;
  3598. sd->sp_vanish_race[type2].per += val;
  3599. }
  3600. break;
  3601. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3602. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3603. if(sd->state.lr_flag != 2) {
  3604. sd->hp_vanish_race[type2].rate += type3;
  3605. sd->hp_vanish_race[type2].per += val;
  3606. }
  3607. break;
  3608. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3609. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3610. if (sd->state.lr_flag == 2)
  3611. break;
  3612. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3613. //if (type3 > sd->norecover_state_race[type2].rate) {
  3614. // sd->norecover_state_race[type2].rate = type3;
  3615. // sd->norecover_state_race[type2].tick = val;
  3616. // break;
  3617. //}
  3618. sd->norecover_state_race[type2].rate = type3;
  3619. sd->norecover_state_race[type2].tick = val;
  3620. break;
  3621. default:
  3622. if (running_npc_stat_calc_event) {
  3623. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3624. }
  3625. else if (current_equip_combo_pos > 0) {
  3626. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3627. }
  3628. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3629. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3630. }
  3631. else {
  3632. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3633. }
  3634. break;
  3635. }
  3636. }
  3637. /**
  3638. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3639. * @param sd
  3640. * @param type Bonus type used by bBonusName
  3641. * @param type2
  3642. * @param type3
  3643. * @param val Value that usually for rate or fixed value
  3644. */
  3645. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3646. {
  3647. nullpo_retv(sd);
  3648. switch(type){
  3649. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3650. if(sd->state.lr_flag != 2)
  3651. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3652. break;
  3653. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3654. if(sd->state.lr_flag != 2)
  3655. 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);
  3656. break;
  3657. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3658. if(sd->state.lr_flag != 2)
  3659. {
  3660. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3661. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3662. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3663. }
  3664. break;
  3665. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3666. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3667. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3668. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3669. break;
  3670. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3671. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3672. if (sd->state.lr_flag != 2)
  3673. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3674. break;
  3675. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3676. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3677. if( sd->state.lr_flag != 2 )
  3678. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3679. break;
  3680. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3681. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3682. if(sd->state.lr_flag == 2)
  3683. break;
  3684. sd->def_set_race[type2].rate = type3;
  3685. sd->def_set_race[type2].tick = type4;
  3686. sd->def_set_race[type2].value = val;
  3687. break;
  3688. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3689. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3690. if(sd->state.lr_flag == 2)
  3691. break;
  3692. sd->mdef_set_race[type2].rate = type3;
  3693. sd->mdef_set_race[type2].tick = type4;
  3694. sd->mdef_set_race[type2].value = val;
  3695. break;
  3696. default:
  3697. if (running_npc_stat_calc_event) {
  3698. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3699. }
  3700. else if (current_equip_combo_pos > 0) {
  3701. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3702. }
  3703. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3704. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3705. }
  3706. else {
  3707. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3708. }
  3709. break;
  3710. }
  3711. }
  3712. /**
  3713. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3714. * @param sd
  3715. * @param type Bonus type used by bBonusName
  3716. * @param type2
  3717. * @param type3
  3718. * @param type4
  3719. * @param val Value that usually for rate or fixed value
  3720. */
  3721. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3722. {
  3723. nullpo_retv(sd);
  3724. switch(type){
  3725. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3726. if(sd->state.lr_flag != 2)
  3727. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3728. break;
  3729. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3730. if(sd->state.lr_flag != 2)
  3731. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3732. break;
  3733. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3734. if(sd->state.lr_flag != 2)
  3735. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3736. break;
  3737. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3738. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3739. if( sd->state.lr_flag != 2 )
  3740. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3741. break;
  3742. default:
  3743. if (running_npc_stat_calc_event) {
  3744. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3745. }
  3746. else if (current_equip_combo_pos > 0) {
  3747. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3748. }
  3749. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3750. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3751. }
  3752. else {
  3753. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3754. }
  3755. break;
  3756. }
  3757. }
  3758. /*==========================================
  3759. * Grants a player a given skill. Flag values are:
  3760. * 0 - Grant permanent skill to be bound to skill tree
  3761. * 1 - Grant an item skill (temporary)
  3762. * 2 - Like 1, except the level granted can stack with previously learned level.
  3763. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3764. *------------------------------------------*/
  3765. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3766. uint16 idx = 0;
  3767. nullpo_ret(sd);
  3768. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3769. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3770. return false;
  3771. }
  3772. if (level > MAX_SKILL_LEVEL) {
  3773. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3774. return false;
  3775. }
  3776. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3777. 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);
  3778. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3779. }
  3780. switch (type) {
  3781. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3782. sd->status.skill[idx].id = skill_id;
  3783. sd->status.skill[idx].lv = level;
  3784. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3785. if (level == 0) { //Remove skill.
  3786. sd->status.skill[idx].id = 0;
  3787. clif_deleteskill(sd,skill_id);
  3788. } else
  3789. clif_addskill(sd,skill_id);
  3790. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3791. status_calc_pc(sd, SCO_NONE);
  3792. break;
  3793. case ADDSKILL_TEMP: //Item bonus skill.
  3794. if (sd->status.skill[idx].id != 0) {
  3795. if (sd->status.skill[idx].lv >= level)
  3796. return true;
  3797. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3798. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3799. } else {
  3800. sd->status.skill[idx].id = skill_id;
  3801. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3802. }
  3803. sd->status.skill[idx].lv = level;
  3804. break;
  3805. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3806. if (sd->status.skill[idx].id != 0) {
  3807. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3808. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3809. } else {
  3810. sd->status.skill[idx].id = skill_id;
  3811. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3812. }
  3813. sd->status.skill[idx].lv += level;
  3814. break;
  3815. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3816. sd->status.skill[idx].id = skill_id;
  3817. sd->status.skill[idx].lv = level;
  3818. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3819. if (level == 0) { //Remove skill.
  3820. sd->status.skill[idx].id = 0;
  3821. clif_deleteskill(sd,skill_id);
  3822. } else
  3823. clif_addskill(sd,skill_id);
  3824. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3825. status_calc_pc(sd, SCO_NONE);
  3826. break;
  3827. default:
  3828. return false;
  3829. }
  3830. return true;
  3831. }
  3832. /*==========================================
  3833. * Append a card to an item ?
  3834. *------------------------------------------*/
  3835. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3836. {
  3837. int i;
  3838. unsigned short nameid;
  3839. nullpo_ret(sd);
  3840. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3841. return 0; //Invalid item index.
  3842. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3843. return 0; //Invalid card index.
  3844. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3845. return 0; // target item missing
  3846. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3847. return 0; // target card missing
  3848. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3849. return 0; // only weapons and armor are allowed
  3850. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3851. return 0; // must be a card
  3852. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3853. return 0; // target must be identified
  3854. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3855. return 0; // card slots reserved for other purposes
  3856. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3857. return 0; // card cannot be compounded on this item type
  3858. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3859. return 0; // attempted to place shield card on left-hand weapon.
  3860. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3861. return 0; // item must be unequipped
  3862. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3863. if( i == sd->inventory_data[idx_equip]->slot )
  3864. return 0; // no free slots
  3865. // remember the card id to insert
  3866. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3867. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3868. {// failed
  3869. clif_insert_card(sd,idx_equip,idx_card,1);
  3870. }
  3871. else
  3872. {// success
  3873. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3874. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3875. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3876. clif_insert_card(sd,idx_equip,idx_card,0);
  3877. }
  3878. return 0;
  3879. }
  3880. //
  3881. // Items
  3882. //
  3883. /*==========================================
  3884. * Update buying value by skills
  3885. *------------------------------------------*/
  3886. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3887. {
  3888. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3889. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3890. rate1 = 5+skill*2-((skill==10)? 1:0);
  3891. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3892. rate2 = 5+skill*4;
  3893. if(rate1 < rate2) rate1 = rate2;
  3894. if(rate1)
  3895. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3896. if(val < 0) val = 0;
  3897. if(orig_value > 0 && val < 1) val = 1;
  3898. return val;
  3899. }
  3900. /*==========================================
  3901. * Update selling value by skills
  3902. *------------------------------------------*/
  3903. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3904. {
  3905. int skill,val = orig_value,rate = 0;
  3906. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3907. rate = 5+skill*2-((skill==10)? 1:0);
  3908. if(rate)
  3909. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3910. if(val < 0) val = 0;
  3911. if(orig_value > 0 && val < 1) val = 1;
  3912. return val;
  3913. }
  3914. /*==========================================
  3915. * Checking if we have enough place on inventory for new item
  3916. * Make sure to take 30k as limit (for client I guess)
  3917. * @param sd
  3918. * @param nameid
  3919. * @param amount
  3920. * @return e_chkitem_result
  3921. *------------------------------------------*/
  3922. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3923. {
  3924. int i;
  3925. struct item_data* data;
  3926. nullpo_ret(sd);
  3927. if(amount > MAX_AMOUNT)
  3928. return CHKADDITEM_OVERAMOUNT;
  3929. data = itemdb_search(nameid);
  3930. if(!itemdb_isstackable2(data))
  3931. return CHKADDITEM_NEW;
  3932. if( data->stack.inventory && amount > data->stack.amount )
  3933. return CHKADDITEM_OVERAMOUNT;
  3934. for(i=0;i<MAX_INVENTORY;i++){
  3935. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3936. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3937. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  3938. return CHKADDITEM_OVERAMOUNT;
  3939. return CHKADDITEM_EXIST;
  3940. }
  3941. }
  3942. return CHKADDITEM_NEW;
  3943. }
  3944. /*==========================================
  3945. * Return number of available place in inventory
  3946. * Each non stackable item will reduce place by 1
  3947. * @param sd
  3948. * @return Number of empty slots
  3949. *------------------------------------------*/
  3950. uint8 pc_inventoryblank(struct map_session_data *sd)
  3951. {
  3952. uint8 i, b;
  3953. nullpo_ret(sd);
  3954. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3955. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3956. b++;
  3957. }
  3958. return b;
  3959. }
  3960. /**
  3961. * Attempts to remove zeny from player
  3962. * @param sd: Player
  3963. * @param zeny: Zeny removed
  3964. * @param type: Log type
  3965. * @param tsd: (optional) From who to log (if null take sd)
  3966. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3967. */
  3968. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3969. {
  3970. nullpo_retr(2,sd);
  3971. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3972. if( zeny < 0 )
  3973. {
  3974. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3975. return 1;
  3976. }
  3977. if( sd->status.zeny < zeny )
  3978. return 1; //Not enough.
  3979. sd->status.zeny -= zeny;
  3980. clif_updatestatus(sd,SP_ZENY);
  3981. if(!tsd) tsd = sd;
  3982. log_zeny(sd, type, tsd, -zeny);
  3983. if( zeny > 0 && sd->state.showzeny ) {
  3984. char output[255];
  3985. sprintf(output, "Removed %dz.", zeny);
  3986. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3987. }
  3988. return 0;
  3989. }
  3990. /**
  3991. * Attempts to give zeny to player
  3992. * @param sd: Player
  3993. * @param type: Log type
  3994. * @param tsd: (optional) From who to log (if null take sd)
  3995. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3996. */
  3997. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3998. {
  3999. nullpo_retr(-1,sd);
  4000. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4001. if( zeny < 0 )
  4002. {
  4003. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4004. return 1;
  4005. }
  4006. if( zeny > MAX_ZENY - sd->status.zeny )
  4007. zeny = MAX_ZENY - sd->status.zeny;
  4008. sd->status.zeny += zeny;
  4009. clif_updatestatus(sd,SP_ZENY);
  4010. if(!tsd) tsd = sd;
  4011. log_zeny(sd, type, tsd, zeny);
  4012. if( zeny > 0 && sd->state.showzeny ) {
  4013. char output[255];
  4014. sprintf(output, "Gained %dz.", zeny);
  4015. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4016. }
  4017. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4018. return 0;
  4019. }
  4020. /**
  4021. * Attempts to remove Cash Points from player
  4022. * @param sd: Player
  4023. * @param price: Points player has to pay
  4024. * @param points: Points player has
  4025. * @param type: Log type
  4026. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  4027. */
  4028. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4029. {
  4030. int cash;
  4031. nullpo_retr(-1,sd);
  4032. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4033. if( price < 0 || points < 0 )
  4034. {
  4035. 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);
  4036. return -2;
  4037. }
  4038. if( points > price )
  4039. {
  4040. 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);
  4041. points = price;
  4042. }
  4043. cash = price-points;
  4044. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4045. {
  4046. 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);
  4047. return -1;
  4048. }
  4049. if( cash ){
  4050. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4051. sd->cashPoints -= cash;
  4052. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4053. }
  4054. if( points ){
  4055. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4056. sd->kafraPoints -= points;
  4057. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4058. }
  4059. if( battle_config.cashshop_show_points )
  4060. {
  4061. char output[CHAT_SIZE_MAX];
  4062. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints); // Used %d kafra points and %d cash points. %d kafra and %d cash points remaining.
  4063. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4064. }
  4065. return cash+points;
  4066. }
  4067. /**
  4068. * Attempts to give Cash Points to player
  4069. * @param sd: Player
  4070. * @param cash: Cash player gets
  4071. * @param points: Points player has
  4072. * @param type: Log type
  4073. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  4074. */
  4075. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4076. {
  4077. char output[CHAT_SIZE_MAX];
  4078. nullpo_retr(-1,sd);
  4079. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4080. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4081. if( cash > 0 )
  4082. {
  4083. if( cash > MAX_ZENY-sd->cashPoints )
  4084. {
  4085. 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);
  4086. cash = MAX_ZENY-sd->cashPoints;
  4087. }
  4088. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4089. sd->cashPoints += cash;
  4090. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4091. if( battle_config.cashshop_show_points )
  4092. {
  4093. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4094. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4095. }
  4096. return cash;
  4097. }
  4098. else if( cash < 0 )
  4099. {
  4100. 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);
  4101. return -1;
  4102. }
  4103. if( points > 0 )
  4104. {
  4105. if( points > MAX_ZENY-sd->kafraPoints )
  4106. {
  4107. 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);
  4108. points = MAX_ZENY-sd->kafraPoints;
  4109. }
  4110. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4111. sd->kafraPoints += points;
  4112. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4113. if( battle_config.cashshop_show_points )
  4114. {
  4115. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4116. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4117. }
  4118. return points;
  4119. }
  4120. else if( points < 0 )
  4121. {
  4122. 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);
  4123. return -1;
  4124. }
  4125. return -2; //shouldn't happen but just in case
  4126. }
  4127. /**
  4128. * Searching a specified itemid in inventory and return his stored index
  4129. * @param sd Player
  4130. * @param nameid Find this Item!
  4131. * @return Stored index in inventory, or -1 if not found.
  4132. **/
  4133. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4134. short i;
  4135. nullpo_retr(-1, sd);
  4136. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4137. return ( i < MAX_INVENTORY ) ? i : -1;
  4138. }
  4139. /** Attempt to add a new item to player inventory
  4140. * @param sd
  4141. * @param item_data
  4142. * @param amount
  4143. * @param log_type
  4144. * @return
  4145. * 0 = success
  4146. * 1 = invalid itemid not found or negative amount
  4147. * 2 = overweight
  4148. * 3 = ?
  4149. * 4 = no free place found
  4150. * 5 = max amount reached
  4151. * 6 = ?
  4152. * 7 = stack limitation
  4153. */
  4154. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4155. struct item_data *id;
  4156. int16 i;
  4157. unsigned int w;
  4158. nullpo_retr(1, sd);
  4159. nullpo_retr(1, item);
  4160. if( item->nameid == 0 || amount <= 0 )
  4161. return ADDITEM_INVALID;
  4162. if( amount > MAX_AMOUNT )
  4163. return ADDITEM_OVERAMOUNT;
  4164. id = itemdb_search(item->nameid);
  4165. if( id->stack.inventory && amount > id->stack.amount )
  4166. {// item stack limitation
  4167. return ADDITEM_STACKLIMIT;
  4168. }
  4169. w = id->weight*amount;
  4170. if(sd->weight + w > sd->max_weight)
  4171. return ADDITEM_OVERWEIGHT;
  4172. i = MAX_INVENTORY;
  4173. if (id->flag.guid && !item->unique_id)
  4174. item->unique_id = pc_generate_unique_id(sd);
  4175. // Stackable | Non Rental
  4176. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4177. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4178. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4179. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4180. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4181. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4182. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4183. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4184. return ADDITEM_OVERAMOUNT;
  4185. sd->inventory.u.items_inventory[i].amount += amount;
  4186. clif_additem(sd,i,amount,0);
  4187. break;
  4188. }
  4189. }
  4190. }
  4191. if (i >= MAX_INVENTORY) {
  4192. i = pc_search_inventory(sd,0);
  4193. if( i < 0 )
  4194. return ADDITEM_OVERITEM;
  4195. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4196. // clear equip and favorite fields first, just in case
  4197. if( item->equip )
  4198. sd->inventory.u.items_inventory[i].equip = 0;
  4199. if( item->favorite )
  4200. sd->inventory.u.items_inventory[i].favorite = 0;
  4201. sd->inventory.u.items_inventory[i].amount = amount;
  4202. sd->inventory_data[i] = id;
  4203. sd->last_addeditem_index = i;
  4204. if (!itemdb_isstackable2(id) || id->flag.guid)
  4205. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4206. clif_additem(sd,i,amount,0);
  4207. }
  4208. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4209. sd->weight += w;
  4210. clif_updatestatus(sd,SP_WEIGHT);
  4211. //Auto-equip
  4212. if(id->flag.autoequip)
  4213. pc_equipitem(sd, i, id->equip);
  4214. /* rental item check */
  4215. if( item->expire_time ) {
  4216. if( time(NULL) > item->expire_time ) {
  4217. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4218. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4219. } else {
  4220. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4221. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4222. pc_inventory_rental_add(sd, seconds);
  4223. }
  4224. }
  4225. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4226. return ADDITEM_SUCCESS;
  4227. }
  4228. /*==========================================
  4229. * Remove an item at index n from inventory by amount.
  4230. * @param sd
  4231. * @param n Item index in inventory
  4232. * @param amount
  4233. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4234. * @param reason Delete reason
  4235. * @param log_type e_log_pick_type
  4236. * @return 1 - invalid itemid or negative amount; 0 - Success
  4237. *------------------------------------------*/
  4238. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4239. {
  4240. nullpo_retr(1, sd);
  4241. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4242. return 1;
  4243. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4244. sd->inventory.u.items_inventory[n].amount -= amount;
  4245. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4246. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4247. if(sd->inventory.u.items_inventory[n].equip)
  4248. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4249. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4250. sd->inventory_data[n] = NULL;
  4251. }
  4252. if(!(type&1))
  4253. clif_delitem(sd,n,amount,reason);
  4254. if(!(type&2))
  4255. clif_updatestatus(sd,SP_WEIGHT);
  4256. return 0;
  4257. }
  4258. /*==========================================
  4259. * Attempt to drop an item.
  4260. * @param sd
  4261. * @param n Item index in inventory
  4262. * @param amount Amount of item
  4263. * @return False = fail; True = success
  4264. *------------------------------------------*/
  4265. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4266. {
  4267. nullpo_retr(1, sd);
  4268. if(n < 0 || n >= MAX_INVENTORY)
  4269. return false;
  4270. if(amount <= 0)
  4271. return false;
  4272. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4273. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4274. sd->inventory.u.items_inventory[n].amount < amount ||
  4275. sd->state.trading || sd->state.vending ||
  4276. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4277. )
  4278. return false;
  4279. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4280. {
  4281. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4282. return false; //Can't drop items in nodrop mapflag maps.
  4283. }
  4284. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4285. {
  4286. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4287. return false;
  4288. }
  4289. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4290. return false;
  4291. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4292. clif_dropitem(sd, n, amount);
  4293. return true;
  4294. }
  4295. /*==========================================
  4296. * Attempt to pick up an item.
  4297. * @param sd
  4298. * @param fitem Item that will be picked
  4299. * @return False = fail; True = success
  4300. *------------------------------------------*/
  4301. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4302. {
  4303. int flag = 0;
  4304. unsigned int tick = gettick();
  4305. struct party_data *p = NULL;
  4306. nullpo_ret(sd);
  4307. nullpo_ret(fitem);
  4308. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4309. return false; // Distance is too far
  4310. if (sd->sc.cant.pickup)
  4311. return false;
  4312. if (sd->status.party_id)
  4313. p = party_search(sd->status.party_id);
  4314. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4315. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4316. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4317. if (!(p && p->party.item&1 &&
  4318. first_sd && first_sd->status.party_id == sd->status.party_id
  4319. ))
  4320. return false;
  4321. }
  4322. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4323. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4324. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4325. if (!(p && p->party.item&1 &&
  4326. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4327. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4328. ))
  4329. return false;
  4330. }
  4331. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4332. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4333. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4334. if(!(p && p->party.item&1 &&
  4335. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4336. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4337. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4338. ))
  4339. return false;
  4340. }
  4341. }
  4342. }
  4343. }
  4344. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4345. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4346. clif_additem(sd,0,0,flag);
  4347. return true;
  4348. }
  4349. //Display pickup animation.
  4350. pc_stop_attack(sd);
  4351. clif_takeitem(&sd->bl,&fitem->bl);
  4352. if (fitem->mob_id &&
  4353. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4354. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4355. )
  4356. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4357. map_clearflooritem(&fitem->bl);
  4358. return true;
  4359. }
  4360. /*==========================================
  4361. * Check if item is usable.
  4362. * Return:
  4363. * false = no
  4364. * true = yes
  4365. *------------------------------------------*/
  4366. bool pc_isUseitem(struct map_session_data *sd,int n)
  4367. {
  4368. struct item_data *item;
  4369. unsigned short nameid;
  4370. nullpo_ret(sd);
  4371. item = sd->inventory_data[n];
  4372. nameid = sd->inventory.u.items_inventory[n].nameid;
  4373. if( item == NULL )
  4374. return false;
  4375. //Not consumable item
  4376. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4377. return false;
  4378. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4379. return true;
  4380. if(map_getmapflag(sd->bl.m, MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  4381. return false;
  4382. //Prevent mass item usage. [Skotlex]
  4383. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4384. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4385. )
  4386. return false;
  4387. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4388. clif_msg(sd,ITEM_NOUSE_SITTING);
  4389. return false; // You cannot use this item while sitting.
  4390. }
  4391. if (sd->state.storage_flag && item->type != IT_CASH) {
  4392. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4393. return false; // You cannot use this item while storage is open.
  4394. }
  4395. if (item->flag.dead_branch && (map_getmapflag(sd->bl.m, MF_NOBRANCH) || map_flag_gvg2(sd->bl.m)))
  4396. return false;
  4397. switch( nameid ) {
  4398. case ITEMID_WING_OF_FLY:
  4399. case ITEMID_GIANT_FLY_WING:
  4400. case ITEMID_N_FLY_WING:
  4401. if( map_getmapflag(sd->bl.m, MF_NOTELEPORT) || map_flag_gvg2(sd->bl.m) ) {
  4402. clif_skill_teleportmessage(sd,0);
  4403. return false;
  4404. }
  4405. if (nameid == ITEMID_GIANT_FLY_WING) {
  4406. struct party_data *pd = party_search(sd->status.party_id);
  4407. if (pd) {
  4408. int i;
  4409. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4410. if (i == MAX_PARTY) { // User is not party leader
  4411. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4412. break;
  4413. }
  4414. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd && pd->data[i].sd != sd && pd->data[i].sd->bl.m == sd->bl.m && !pc_isdead(pd->data[i].sd));
  4415. if (i == MAX_PARTY) { // No party members found on same map
  4416. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4417. break;
  4418. }
  4419. } else {
  4420. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4421. break;
  4422. }
  4423. }
  4424. // Fall through
  4425. case ITEMID_WING_OF_BUTTERFLY:
  4426. case ITEMID_N_BUTTERFLY_WING:
  4427. case ITEMID_DUN_TELE_SCROLL1:
  4428. case ITEMID_DUN_TELE_SCROLL2:
  4429. case ITEMID_DUN_TELE_SCROLL3:
  4430. case ITEMID_WOB_RUNE:
  4431. case ITEMID_WOB_SCHWALTZ:
  4432. case ITEMID_WOB_RACHEL:
  4433. case ITEMID_WOB_LOCAL:
  4434. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4435. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4436. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4437. return false;
  4438. }
  4439. if( map_getmapflag(sd->bl.m, MF_NORETURN) && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4440. return false;
  4441. break;
  4442. case ITEMID_MERCENARY_RED_POTION:
  4443. case ITEMID_MERCENARY_BLUE_POTION:
  4444. case ITEMID_M_CENTER_POTION:
  4445. case ITEMID_M_AWAKENING_POTION:
  4446. case ITEMID_M_BERSERK_POTION:
  4447. if( sd->md == NULL || sd->md->db == NULL )
  4448. return false;
  4449. if( sd->md->sc.data[SC_BERSERK] )
  4450. return false;
  4451. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4452. return false;
  4453. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4454. return false;
  4455. break;
  4456. case ITEMID_NEURALIZER:
  4457. if( !map_getmapflag(sd->bl.m, MF_RESET) )
  4458. return false;
  4459. break;
  4460. }
  4461. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4462. return false; // Mercenary Scrolls
  4463. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4464. if( pc_is90overweight(sd) ) {
  4465. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4466. return false;
  4467. }
  4468. if( !pc_inventoryblank(sd) ) {
  4469. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4470. return false;
  4471. }
  4472. }
  4473. //Gender check
  4474. if(item->sex != 2 && sd->status.sex != item->sex)
  4475. return false;
  4476. //Required level check
  4477. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4478. return false;
  4479. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4480. return false;
  4481. //Not equipable by class. [Skotlex]
  4482. if (!(
  4483. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4484. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4485. ))
  4486. return false;
  4487. if (sd->sc.count && (
  4488. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4489. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4490. sd->sc.data[SC_TRICKDEAD] ||
  4491. sd->sc.data[SC_HIDING] ||
  4492. sd->sc.data[SC__SHADOWFORM] ||
  4493. sd->sc.data[SC__INVISIBILITY] ||
  4494. sd->sc.data[SC__MANHOLE] ||
  4495. sd->sc.data[SC_DEEPSLEEP] ||
  4496. sd->sc.data[SC_CRYSTALIZE] ||
  4497. sd->sc.data[SC_KAGEHUMI] ||
  4498. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4499. sd->sc.data[SC_KINGS_GRACE] ||
  4500. sd->sc.data[SC_SUHIDE]))
  4501. return false;
  4502. if (!pc_isItemClass(sd,item))
  4503. return false;
  4504. //Dead Branch items
  4505. if( item->flag.dead_branch )
  4506. log_branch(sd);
  4507. return true;
  4508. }
  4509. /*==========================================
  4510. * Last checks to use an item.
  4511. * Return:
  4512. * 0 = fail
  4513. * 1 = success
  4514. *------------------------------------------*/
  4515. int pc_useitem(struct map_session_data *sd,int n)
  4516. {
  4517. unsigned int tick = gettick();
  4518. int amount;
  4519. unsigned short nameid;
  4520. struct script_code *script;
  4521. struct item item;
  4522. struct item_data *id;
  4523. nullpo_ret(sd);
  4524. if (sd->npc_id) {
  4525. if (sd->progressbar.npc_id) {
  4526. clif_progressbar_abort(sd);
  4527. return 0; // First item use attempt cancels the progress bar
  4528. }
  4529. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4530. #ifdef RENEWAL
  4531. clif_msg( sd, WORK_IN_PROGRESS );
  4532. #endif
  4533. return 0;
  4534. }
  4535. }
  4536. item = sd->inventory.u.items_inventory[n];
  4537. id = sd->inventory_data[n];
  4538. if (item.nameid == 0 || item.amount <= 0)
  4539. return 0;
  4540. if( !pc_isUseitem(sd,n) )
  4541. return 0;
  4542. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4543. nameid = id->nameid;
  4544. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4545. return 0;
  4546. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4547. if( id->flag.delay_consume ) {
  4548. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4549. return 0;
  4550. else if( pc_issit(sd) )
  4551. return 0;
  4552. }
  4553. //Since most delay-consume items involve using a "skill-type" target cursor,
  4554. //perform a skill-use check before going through. [Skotlex]
  4555. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4556. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4557. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4558. return 0;
  4559. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4560. return 0;
  4561. /* on restricted maps the item is consumed but the effect is not used */
  4562. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4563. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4564. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4565. clif_useitemack(sd,n,item.amount-1,true);
  4566. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4567. }
  4568. return 0;/* regardless, effect is not run */
  4569. }
  4570. sd->itemid = item.nameid;
  4571. sd->itemindex = n;
  4572. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4573. sd->catch_target_class = PET_CATCH_FAIL;
  4574. amount = item.amount;
  4575. script = id->script;
  4576. //Check if the item is to be consumed immediately [Skotlex]
  4577. if (id->flag.delay_consume)
  4578. clif_useitemack(sd, n, amount, true);
  4579. else
  4580. {
  4581. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4582. {
  4583. clif_useitemack(sd, n, amount - 1, true);
  4584. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4585. }
  4586. else
  4587. clif_useitemack(sd, n, 0, false);
  4588. }
  4589. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4590. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4591. //Update item use time.
  4592. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4593. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4594. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4595. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4596. potion_flag = 0;
  4597. return 1;
  4598. }
  4599. /**
  4600. * Add item on cart for given index.
  4601. * @param sd
  4602. * @param item
  4603. * @param amount
  4604. * @param log_type
  4605. * @return 0 = success; 1 = fail; 2 = no slot
  4606. */
  4607. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4608. {
  4609. struct item_data *data;
  4610. int i,w;
  4611. nullpo_retr(1, sd);
  4612. nullpo_retr(1, item);
  4613. if(item->nameid == 0 || amount <= 0)
  4614. return 1;
  4615. data = itemdb_search(item->nameid);
  4616. if( data->stack.cart && amount > data->stack.amount )
  4617. {// item stack limitation
  4618. return 1;
  4619. }
  4620. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4621. { // Check item trade restrictions [Skotlex]
  4622. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4623. return 1;
  4624. }
  4625. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4626. return 1;
  4627. i = MAX_CART;
  4628. if( itemdb_isstackable2(data) && !item->expire_time )
  4629. {
  4630. for (i = 0; i < MAX_CART; i++) {
  4631. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4632. && sd->cart.u.items_cart[i].bound == item->bound
  4633. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4634. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4635. )
  4636. break;
  4637. }
  4638. }
  4639. if( i < MAX_CART )
  4640. {// item already in cart, stack it
  4641. if( amount > MAX_AMOUNT - sd->cart.u.items_cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->cart.u.items_cart[i].amount ) )
  4642. return 2; // no slot
  4643. sd->cart.u.items_cart[i].amount += amount;
  4644. clif_cart_additem(sd,i,amount,0);
  4645. }
  4646. else
  4647. {// item not stackable or not present, add it
  4648. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4649. if( i == MAX_CART )
  4650. return 2; // no slot
  4651. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4652. sd->cart.u.items_cart[i].id = 0;
  4653. sd->cart.u.items_cart[i].amount = amount;
  4654. sd->cart_num++;
  4655. clif_cart_additem(sd,i,amount,0);
  4656. }
  4657. sd->cart.u.items_cart[i].favorite = 0; // clear
  4658. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4659. sd->cart_weight += w;
  4660. clif_updatestatus(sd,SP_CARTINFO);
  4661. return 0;
  4662. }
  4663. /*==========================================
  4664. * Delete item on cart for given index.
  4665. *------------------------------------------*/
  4666. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4667. {
  4668. nullpo_retv(sd);
  4669. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4670. sd->cart.u.items_cart[n].amount < amount)
  4671. return;
  4672. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4673. sd->cart.u.items_cart[n].amount -= amount;
  4674. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4675. if(sd->cart.u.items_cart[n].amount <= 0) {
  4676. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4677. sd->cart_num--;
  4678. }
  4679. if(!type) {
  4680. clif_cart_delitem(sd,n,amount);
  4681. clif_updatestatus(sd,SP_CARTINFO);
  4682. }
  4683. }
  4684. /*==========================================
  4685. * Transfer item from inventory to cart.
  4686. *------------------------------------------*/
  4687. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4688. {
  4689. struct item *item_data;
  4690. char flag;
  4691. nullpo_retv(sd);
  4692. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4693. return;
  4694. item_data = &sd->inventory.u.items_inventory[idx];
  4695. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4696. return;
  4697. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4698. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4699. else {
  4700. clif_dropitem(sd,idx,0);
  4701. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4702. }
  4703. }
  4704. /*==========================================
  4705. * Get number of item in cart.
  4706. * Return:
  4707. -1 = itemid not found or no amount found
  4708. x = remaining itemid on cart after get
  4709. *------------------------------------------*/
  4710. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4711. {
  4712. struct item* item_data;
  4713. nullpo_retr(-1, sd);
  4714. item_data = &sd->cart.u.items_cart[idx];
  4715. if( item_data->nameid == 0 || item_data->amount == 0 )
  4716. return -1;
  4717. return item_data->amount - amount;
  4718. }
  4719. /*==========================================
  4720. * Retrieve an item at index idx from cart.
  4721. *------------------------------------------*/
  4722. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4723. {
  4724. struct item *item_data;
  4725. unsigned char flag = 0;
  4726. nullpo_retv(sd);
  4727. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4728. return;
  4729. item_data=&sd->cart.u.items_cart[idx];
  4730. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4731. return;
  4732. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4733. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4734. else {
  4735. clif_dropitem(sd,idx,0);
  4736. clif_additem(sd,0,0,flag);
  4737. }
  4738. }
  4739. /*==========================================
  4740. * Bound Item Check
  4741. * Type:
  4742. * 1 Account Bound
  4743. * 2 Guild Bound
  4744. * 3 Party Bound
  4745. * 4 Character Bound
  4746. *------------------------------------------*/
  4747. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4748. {
  4749. int i = 0, j = 0;
  4750. for(i = 0; i < MAX_INVENTORY; i++) {
  4751. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0 && sd->inventory.u.items_inventory[i].bound == type) {
  4752. idxlist[j] = i;
  4753. j++;
  4754. }
  4755. }
  4756. return j;
  4757. }
  4758. /*==========================================
  4759. * Display item stolen msg to player sd
  4760. *------------------------------------------*/
  4761. int pc_show_steal(struct block_list *bl,va_list ap)
  4762. {
  4763. struct map_session_data *sd;
  4764. int itemid;
  4765. struct item_data *item=NULL;
  4766. char output[100];
  4767. sd=va_arg(ap,struct map_session_data *);
  4768. itemid=va_arg(ap,int);
  4769. if((item=itemdb_exists(itemid))==NULL)
  4770. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4771. else
  4772. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4773. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4774. return 0;
  4775. }
  4776. /*==========================================
  4777. * Steal an item from bl (mob).
  4778. * Return:
  4779. * 0 = fail
  4780. * 1 = succes
  4781. *------------------------------------------*/
  4782. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4783. {
  4784. int i,itemid;
  4785. double rate;
  4786. unsigned char flag = 0;
  4787. struct status_data *sd_status, *md_status;
  4788. struct mob_data *md;
  4789. struct item tmp_item;
  4790. if(!sd || !bl || bl->type!=BL_MOB)
  4791. return 0;
  4792. md = (TBL_MOB *)bl;
  4793. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4794. return 0;
  4795. sd_status= status_get_status_data(&sd->bl);
  4796. md_status= status_get_status_data(bl);
  4797. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4798. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4799. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4800. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4801. ) { //Can't steal from
  4802. md->state.steal_flag = UCHAR_MAX;
  4803. return 0;
  4804. }
  4805. // base skill success chance (percentual)
  4806. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4807. rate += sd->bonus.add_steal_rate;
  4808. if( rate < 1 )
  4809. return 0;
  4810. // Try dropping one item, in the order from first to last possible slot.
  4811. // Droprate is affected by the skill success rate.
  4812. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4813. if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4814. break;
  4815. if( i == MAX_STEAL_DROP )
  4816. return 0;
  4817. itemid = md->db->dropitem[i].nameid;
  4818. memset(&tmp_item,0,sizeof(tmp_item));
  4819. tmp_item.nameid = itemid;
  4820. tmp_item.amount = 1;
  4821. tmp_item.identify = itemdb_isidentified(itemid);
  4822. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4823. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4824. //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?
  4825. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4826. if(flag) { //Failed to steal due to overweight
  4827. clif_additem(sd,0,0,flag);
  4828. return 0;
  4829. }
  4830. if(battle_config.show_steal_in_same_party)
  4831. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4832. //Logs items, Stolen from mobs [Lupus]
  4833. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4834. //A Rare Steal Global Announce by Lupus
  4835. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4836. struct item_data *i_data;
  4837. char message[128];
  4838. i_data = itemdb_search(itemid);
  4839. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4840. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4841. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4842. }
  4843. return 1;
  4844. }
  4845. /*==========================================
  4846. * Stole zeny from bl (mob)
  4847. * return
  4848. * 0 = fail
  4849. * 1 = success
  4850. *------------------------------------------*/
  4851. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4852. {
  4853. int rate, target_lv;
  4854. struct mob_data *md;
  4855. if(!sd || !target || target->type != BL_MOB)
  4856. return 0;
  4857. md = (TBL_MOB*)target;
  4858. target_lv = status_get_lv(target);
  4859. 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)
  4860. return 0;
  4861. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4862. if(rnd()%1000 < rate)
  4863. {
  4864. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4865. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4866. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4867. md->state.steal_coin_flag = 1;
  4868. return 1;
  4869. }
  4870. return 0;
  4871. }
  4872. /*==========================================
  4873. * Set's a player position.
  4874. * @param sd
  4875. * @param mapindex
  4876. * @param x
  4877. * @param y
  4878. * @param clrtype
  4879. * @return SETPOS_OK Success
  4880. * SETPOS_MAPINDEX Invalid map index
  4881. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4882. * SETPOS_AUTOTRADE Player is in autotrade state
  4883. *------------------------------------------*/
  4884. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4885. {
  4886. int16 m;
  4887. nullpo_retr(SETPOS_OK,sd);
  4888. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4889. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4890. return SETPOS_MAPINDEX;
  4891. }
  4892. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4893. return SETPOS_AUTOTRADE;
  4894. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4895. pc_setstand(sd, true);
  4896. pc_setrestartvalue(sd,1);
  4897. }
  4898. m = map_mapindex2mapid(mapindex);
  4899. struct map_data *mapdata = map_getmapdata(m);
  4900. sd->state.changemap = (sd->mapindex != mapindex);
  4901. sd->state.warping = 1;
  4902. sd->state.workinprogress = WIP_DISABLE_NONE;
  4903. if( sd->state.changemap ) { // Misc map-changing settings
  4904. int i;
  4905. if(map_getmapdata(sd->bl.m)->instance_id && !mapdata->instance_id) {
  4906. bool instance_found = false;
  4907. struct party_data *p = NULL;
  4908. struct guild *g = NULL;
  4909. if (sd->instance_id) {
  4910. instance_delusers(sd->instance_id);
  4911. instance_found = true;
  4912. }
  4913. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4914. instance_delusers(p->instance_id);
  4915. instance_found = true;
  4916. }
  4917. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4918. instance_delusers(g->instance_id);
  4919. }
  4920. sd->state.pmap = sd->bl.m;
  4921. if (sd->sc.count) { // Cancel some map related stuff.
  4922. if (sd->sc.data[SC_JAILED])
  4923. return SETPOS_MAPINDEX; //You may not get out!
  4924. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4925. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4926. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4927. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4928. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4929. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4930. if (sd->sc.data[SC_KNOWLEDGE]) {
  4931. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4932. if (sce->timer != INVALID_TIMER)
  4933. delete_timer(sce->timer, status_change_timer);
  4934. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4935. }
  4936. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4937. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4938. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4939. }
  4940. for( i = 0; i < EQI_MAX; i++ ) {
  4941. if( sd->equip_index[i] >= 0 )
  4942. if( pc_isequip(sd,sd->equip_index[i]) )
  4943. pc_unequipitem(sd,sd->equip_index[i],2);
  4944. }
  4945. if (battle_config.clear_unit_onwarp&BL_PC)
  4946. skill_clear_unitgroup(&sd->bl);
  4947. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4948. guild_send_dot_remove(sd);
  4949. bg_send_dot_remove(sd);
  4950. if (sd->regen.state.gc)
  4951. sd->regen.state.gc = 0;
  4952. // make sure vending is allowed here
  4953. if (sd->state.vending && map_getmapflag(m, MF_NOVENDING)) {
  4954. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4955. vending_closevending(sd);
  4956. }
  4957. channel_pcquit(sd,4); //quit map chan
  4958. }
  4959. if( m < 0 )
  4960. {
  4961. uint32 ip;
  4962. uint16 port;
  4963. struct script_state *st;
  4964. //if can't find any map-servers, just abort setting position.
  4965. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4966. return SETPOS_NO_MAPSERVER;
  4967. if (sd->npc_id){
  4968. npc_event_dequeue(sd,false);
  4969. st = sd->st;
  4970. }else{
  4971. st = nullptr;
  4972. }
  4973. npc_script_event(sd, NPCE_LOGOUT);
  4974. //remove from map, THEN change x/y coordinates
  4975. unit_remove_map_pc(sd,clrtype);
  4976. sd->mapindex = mapindex;
  4977. sd->bl.x=x;
  4978. sd->bl.y=y;
  4979. pc_clean_skilltree(sd);
  4980. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4981. chrif_changemapserver(sd, ip, (short)port);
  4982. //Free session data from this map server [Kevin]
  4983. unit_free_pc(sd);
  4984. if( st ){
  4985. // Has to be done here, because otherwise unit_free_pc will free the stack already
  4986. st->state = END;
  4987. }
  4988. return SETPOS_OK;
  4989. }
  4990. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  4991. {
  4992. 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);
  4993. x = y = 0; // make it random
  4994. }
  4995. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4996. int c=0;
  4997. do {
  4998. x = rnd()%(mapdata->xs-2)+1;
  4999. y = rnd()%(mapdata->ys-2)+1;
  5000. c++;
  5001. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5002. 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));
  5003. return SETPOS_OK; //preventing warp
  5004. //break; //allow warp anyway
  5005. }
  5006. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5007. }
  5008. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5009. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5010. vending_closevending(sd);
  5011. }
  5012. if(sd->bl.prev != NULL){
  5013. unit_remove_map_pc(sd,clrtype);
  5014. clif_changemap(sd,m,x,y); // [MouseJstr]
  5015. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5016. sd->state.rewarp = 1;
  5017. sd->mapindex = mapindex;
  5018. sd->bl.m = m;
  5019. sd->bl.x = sd->ud.to_x = x;
  5020. sd->bl.y = sd->ud.to_y = y;
  5021. if( sd->status.guild_id > 0 && map_getmapflag(m, MF_GVG_CASTLE) )
  5022. { // Increased guild castle regen [Valaris]
  5023. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5024. if(gc && gc->guild_id == sd->status.guild_id)
  5025. sd->regen.state.gc = 1;
  5026. }
  5027. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5028. {
  5029. sd->pd->bl.m = m;
  5030. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5031. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5032. sd->pd->ud.dir = sd->ud.dir;
  5033. }
  5034. if( hom_is_active(sd->hd) )
  5035. {
  5036. sd->hd->bl.m = m;
  5037. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5038. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5039. sd->hd->ud.dir = sd->ud.dir;
  5040. }
  5041. if( sd->md )
  5042. {
  5043. sd->md->bl.m = m;
  5044. sd->md->bl.x = sd->md->ud.to_x = x;
  5045. sd->md->bl.y = sd->md->ud.to_y = y;
  5046. sd->md->ud.dir = sd->ud.dir;
  5047. }
  5048. if( sd->ed ) {
  5049. sd->ed->bl.m = m;
  5050. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5051. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5052. sd->ed->ud.dir = sd->ud.dir;
  5053. }
  5054. pc_cell_basilica(sd);
  5055. //check if we gonna be rewarped [lighta]
  5056. if(npc_check_areanpc(1,m,x,y,0)){
  5057. sd->count_rewarp++;
  5058. }
  5059. else
  5060. sd->count_rewarp = 0;
  5061. return SETPOS_OK;
  5062. }
  5063. /*==========================================
  5064. * Warp player sd to random location on current map.
  5065. * May fail if no walkable cell found (1000 attempts).
  5066. * Return:
  5067. * 0 = Success
  5068. * 1,2,3 = Fail
  5069. *------------------------------------------*/
  5070. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5071. {
  5072. int x,y,i=0;
  5073. nullpo_ret(sd);
  5074. if (map_getmapflag(sd->bl.m, MF_NOTELEPORT)) //Teleport forbidden
  5075. return 3;
  5076. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5077. do {
  5078. x = rnd()%(mapdata->xs-2)+1;
  5079. y = rnd()%(mapdata->ys-2)+1;
  5080. } while((map_getcell(sd->bl.m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,sd->bl.m,x,y,1))) && (i++) < 1000);
  5081. if (i < 1000)
  5082. return pc_setpos(sd,mapdata->index,x,y,type);
  5083. return 3;
  5084. }
  5085. /*==========================================
  5086. * Records a memo point at sd's current position
  5087. * pos - entry to replace, (-1: shift oldest entry out)
  5088. *------------------------------------------*/
  5089. bool pc_memo(struct map_session_data* sd, int pos)
  5090. {
  5091. int skill;
  5092. nullpo_ret(sd);
  5093. // check mapflags
  5094. if( sd->bl.m >= 0 && (map_getmapflag(sd->bl.m, MF_NOMEMO) || map_getmapflag(sd->bl.m, MF_NOWARPTO)) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  5095. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5096. return false;
  5097. }
  5098. // check inputs
  5099. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5100. return false; // invalid input
  5101. // check required skill level
  5102. skill = pc_checkskill(sd, AL_WARP);
  5103. if( skill < 1 ) {
  5104. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5105. return false;
  5106. }
  5107. if( skill < 2 || skill - 2 < pos ) {
  5108. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5109. return false;
  5110. }
  5111. if( pos == -1 )
  5112. {
  5113. uint8 i;
  5114. // prevent memo-ing the same map multiple times
  5115. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5116. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5117. pos = 0;
  5118. }
  5119. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5120. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5121. return false;
  5122. }
  5123. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5124. sd->status.memo_point[pos].x = sd->bl.x;
  5125. sd->status.memo_point[pos].y = sd->bl.y;
  5126. clif_skill_memomessage(sd, 0);
  5127. return true;
  5128. }
  5129. //
  5130. // Skills
  5131. //
  5132. /**
  5133. * Get the skill current cooldown for player.
  5134. * (get the db base cooldown for skill + player specific cooldown)
  5135. * @param sd : player pointer
  5136. * @param id : skill id
  5137. * @param lv : skill lv
  5138. * @return player skill cooldown
  5139. */
  5140. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5141. uint8 i;
  5142. uint16 idx = skill_get_index(skill_id);
  5143. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5144. if (!idx) return 0;
  5145. if (skill_db[idx]->cooldown[skill_lv - 1])
  5146. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5147. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5148. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5149. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5150. if (i < cooldownlen) {
  5151. cooldown += sd->skillcooldown[i].val;
  5152. cooldown = max(0,cooldown);
  5153. }
  5154. return cooldown;
  5155. }
  5156. /*==========================================
  5157. * Return player sd skill_lv learned for given skill
  5158. *------------------------------------------*/
  5159. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5160. {
  5161. uint16 idx = 0;
  5162. if (sd == NULL)
  5163. return 0;
  5164. if ((idx = skill_get_index(skill_id)) == 0) {
  5165. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5166. return 0;
  5167. }
  5168. if (SKILL_CHK_GUILD(skill_id) ) {
  5169. struct guild *g;
  5170. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5171. return guild_checkskill(g,skill_id);
  5172. return 0;
  5173. }
  5174. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5175. }
  5176. /**
  5177. * Check if we still have the correct weapon to continue the skill (actually status)
  5178. * If not ending it
  5179. * @param sd
  5180. * @return 0:error, 1:check done
  5181. */
  5182. static void pc_checkallowskill(struct map_session_data *sd)
  5183. {
  5184. const enum sc_type scw_list[] = {
  5185. SC_TWOHANDQUICKEN,
  5186. SC_ONEHAND,
  5187. SC_AURABLADE,
  5188. SC_PARRYING,
  5189. SC_SPEARQUICKEN,
  5190. SC_ADRENALINE,
  5191. SC_ADRENALINE2,
  5192. SC_DANCING,
  5193. SC_GATLINGFEVER,
  5194. };
  5195. uint8 i;
  5196. nullpo_retv(sd);
  5197. if(!sd->sc.count)
  5198. return;
  5199. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5200. { // Skills requiring specific weapon types
  5201. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5202. continue;
  5203. if(sd->sc.data[scw_list[i]] &&
  5204. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5205. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5206. }
  5207. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5208. // Spurt requires bare hands (feet, in fact xD)
  5209. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5210. if(sd->status.shield <= 0) { // Skills requiring a shield
  5211. const enum sc_type scs_list[] = {
  5212. SC_AUTOGUARD,
  5213. SC_DEFENDER,
  5214. SC_REFLECTSHIELD,
  5215. SC_REFLECTDAMAGE
  5216. };
  5217. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5218. if(sd->sc.data[scs_list[i]])
  5219. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5220. }
  5221. }
  5222. /*==========================================
  5223. * Return equipped index of item on player sd at pos
  5224. * Return
  5225. * -1 : Nothing equipped
  5226. * idx : (this index could be used in inventory to found item_data)
  5227. *------------------------------------------*/
  5228. short pc_checkequip(struct map_session_data *sd,int pos)
  5229. {
  5230. uint8 i;
  5231. nullpo_retr(-1, sd);
  5232. for(i=0;i<EQI_MAX;i++){
  5233. if(pos & equip_bitmask[i])
  5234. return sd->equip_index[i];
  5235. }
  5236. return -1;
  5237. }
  5238. /*==========================================
  5239. * Check if sd has nameid equipped somewhere
  5240. * @sd : the player session
  5241. * @nameid : id of the item to check
  5242. * @min : : see pc.hpp enum equip_index from ? to @max
  5243. * @max : see pc.hpp enum equip_index for @min to ?
  5244. * -return true,false
  5245. *------------------------------------------*/
  5246. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5247. {
  5248. int i;
  5249. for(i = min; i < max; i++) {
  5250. if(equip_bitmask[i]) {
  5251. int idx = sd->equip_index[i];
  5252. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5253. return true;
  5254. }
  5255. }
  5256. return false;
  5257. }
  5258. /*==========================================
  5259. * Convert's from the client's lame Job ID system
  5260. * to the map server's 'makes sense' system. [Skotlex]
  5261. *------------------------------------------*/
  5262. int pc_jobid2mapid(unsigned short b_class)
  5263. {
  5264. switch(b_class)
  5265. {
  5266. //Novice And 1-1 Jobs
  5267. case JOB_NOVICE: return MAPID_NOVICE;
  5268. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5269. case JOB_MAGE: return MAPID_MAGE;
  5270. case JOB_ARCHER: return MAPID_ARCHER;
  5271. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5272. case JOB_MERCHANT: return MAPID_MERCHANT;
  5273. case JOB_THIEF: return MAPID_THIEF;
  5274. case JOB_TAEKWON: return MAPID_TAEKWON;
  5275. case JOB_WEDDING: return MAPID_WEDDING;
  5276. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5277. case JOB_NINJA: return MAPID_NINJA;
  5278. case JOB_XMAS: return MAPID_XMAS;
  5279. case JOB_SUMMER: return MAPID_SUMMER;
  5280. case JOB_HANBOK: return MAPID_HANBOK;
  5281. case JOB_GANGSI: return MAPID_GANGSI;
  5282. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5283. case JOB_SUMMER2: return MAPID_SUMMER2;
  5284. //2-1 Jobs
  5285. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5286. case JOB_KNIGHT: return MAPID_KNIGHT;
  5287. case JOB_WIZARD: return MAPID_WIZARD;
  5288. case JOB_HUNTER: return MAPID_HUNTER;
  5289. case JOB_PRIEST: return MAPID_PRIEST;
  5290. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5291. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5292. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5293. case JOB_KAGEROU:
  5294. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5295. case JOB_REBELLION: return MAPID_REBELLION;
  5296. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5297. //2-2 Jobs
  5298. case JOB_CRUSADER: return MAPID_CRUSADER;
  5299. case JOB_SAGE: return MAPID_SAGE;
  5300. case JOB_BARD:
  5301. case JOB_DANCER: return MAPID_BARDDANCER;
  5302. case JOB_MONK: return MAPID_MONK;
  5303. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5304. case JOB_ROGUE: return MAPID_ROGUE;
  5305. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5306. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5307. //Trans Novice And Trans 1-1 Jobs
  5308. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5309. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5310. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5311. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5312. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5313. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5314. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5315. //Trans 2-1 Jobs
  5316. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5317. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5318. case JOB_SNIPER: return MAPID_SNIPER;
  5319. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5320. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5321. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5322. //Trans 2-2 Jobs
  5323. case JOB_PALADIN: return MAPID_PALADIN;
  5324. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5325. case JOB_CLOWN:
  5326. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5327. case JOB_CHAMPION: return MAPID_CHAMPION;
  5328. case JOB_CREATOR: return MAPID_CREATOR;
  5329. case JOB_STALKER: return MAPID_STALKER;
  5330. //Baby Novice And Baby 1-1 Jobs
  5331. case JOB_BABY: return MAPID_BABY;
  5332. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5333. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5334. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5335. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5336. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5337. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5338. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5339. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5340. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5341. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5342. //Baby 2-1 Jobs
  5343. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5344. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5345. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5346. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5347. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5348. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5349. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5350. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5351. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5352. case JOB_BABY_KAGEROU:
  5353. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5354. //Baby 2-2 Jobs
  5355. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5356. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5357. case JOB_BABY_BARD:
  5358. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5359. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5360. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5361. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5362. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5363. //3-1 Jobs
  5364. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5365. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5366. case JOB_WARLOCK: return MAPID_WARLOCK;
  5367. case JOB_RANGER: return MAPID_RANGER;
  5368. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5369. case JOB_MECHANIC: return MAPID_MECHANIC;
  5370. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5371. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5372. //3-2 Jobs
  5373. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5374. case JOB_SORCERER: return MAPID_SORCERER;
  5375. case JOB_MINSTREL:
  5376. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5377. case JOB_SURA: return MAPID_SURA;
  5378. case JOB_GENETIC: return MAPID_GENETIC;
  5379. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5380. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5381. //Trans 3-1 Jobs
  5382. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5383. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5384. case JOB_RANGER_T: return MAPID_RANGER_T;
  5385. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5386. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5387. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5388. //Trans 3-2 Jobs
  5389. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5390. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5391. case JOB_MINSTREL_T:
  5392. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5393. case JOB_SURA_T: return MAPID_SURA_T;
  5394. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5395. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5396. //Baby 3-1 Jobs
  5397. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5398. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5399. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5400. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5401. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5402. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5403. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5404. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5405. //Baby 3-2 Jobs
  5406. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5407. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5408. case JOB_BABY_MINSTREL:
  5409. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5410. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5411. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5412. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5413. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5414. //Doram Jobs
  5415. case JOB_SUMMONER: return MAPID_SUMMONER;
  5416. default:
  5417. return -1;
  5418. }
  5419. }
  5420. //Reverts the map-style class id to the client-style one.
  5421. int pc_mapid2jobid(unsigned short class_, int sex)
  5422. {
  5423. switch(class_) {
  5424. //Novice And 1-1 Jobs
  5425. case MAPID_NOVICE: return JOB_NOVICE;
  5426. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5427. case MAPID_MAGE: return JOB_MAGE;
  5428. case MAPID_ARCHER: return JOB_ARCHER;
  5429. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5430. case MAPID_MERCHANT: return JOB_MERCHANT;
  5431. case MAPID_THIEF: return JOB_THIEF;
  5432. case MAPID_TAEKWON: return JOB_TAEKWON;
  5433. case MAPID_WEDDING: return JOB_WEDDING;
  5434. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5435. case MAPID_NINJA: return JOB_NINJA;
  5436. case MAPID_XMAS: return JOB_XMAS;
  5437. case MAPID_SUMMER: return JOB_SUMMER;
  5438. case MAPID_HANBOK: return JOB_HANBOK;
  5439. case MAPID_GANGSI: return JOB_GANGSI;
  5440. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5441. case MAPID_SUMMER2: return JOB_SUMMER2;
  5442. //2-1 Jobs
  5443. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5444. case MAPID_KNIGHT: return JOB_KNIGHT;
  5445. case MAPID_WIZARD: return JOB_WIZARD;
  5446. case MAPID_HUNTER: return JOB_HUNTER;
  5447. case MAPID_PRIEST: return JOB_PRIEST;
  5448. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5449. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5450. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5451. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5452. case MAPID_REBELLION: return JOB_REBELLION;
  5453. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5454. //2-2 Jobs
  5455. case MAPID_CRUSADER: return JOB_CRUSADER;
  5456. case MAPID_SAGE: return JOB_SAGE;
  5457. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5458. case MAPID_MONK: return JOB_MONK;
  5459. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5460. case MAPID_ROGUE: return JOB_ROGUE;
  5461. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5462. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5463. //Trans Novice And Trans 2-1 Jobs
  5464. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5465. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5466. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5467. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5468. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5469. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5470. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5471. //Trans 2-1 Jobs
  5472. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5473. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5474. case MAPID_SNIPER: return JOB_SNIPER;
  5475. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5476. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5477. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5478. //Trans 2-2 Jobs
  5479. case MAPID_PALADIN: return JOB_PALADIN;
  5480. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5481. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5482. case MAPID_CHAMPION: return JOB_CHAMPION;
  5483. case MAPID_CREATOR: return JOB_CREATOR;
  5484. case MAPID_STALKER: return JOB_STALKER;
  5485. //Baby Novice And Baby 1-1 Jobs
  5486. case MAPID_BABY: return JOB_BABY;
  5487. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5488. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5489. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5490. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5491. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5492. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5493. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5494. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5495. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5496. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5497. //Baby 2-1 Jobs
  5498. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5499. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5500. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5501. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5502. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5503. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5504. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5505. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5506. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5507. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5508. //Baby 2-2 Jobs
  5509. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5510. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5511. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5512. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5513. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5514. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5515. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5516. //3-1 Jobs
  5517. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5518. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5519. case MAPID_WARLOCK: return JOB_WARLOCK;
  5520. case MAPID_RANGER: return JOB_RANGER;
  5521. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5522. case MAPID_MECHANIC: return JOB_MECHANIC;
  5523. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5524. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5525. //3-2 Jobs
  5526. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5527. case MAPID_SORCERER: return JOB_SORCERER;
  5528. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5529. case MAPID_SURA: return JOB_SURA;
  5530. case MAPID_GENETIC: return JOB_GENETIC;
  5531. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5532. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5533. //Trans 3-1 Jobs
  5534. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5535. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5536. case MAPID_RANGER_T: return JOB_RANGER_T;
  5537. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5538. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5539. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5540. //Trans 3-2 Jobs
  5541. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5542. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5543. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5544. case MAPID_SURA_T: return JOB_SURA_T;
  5545. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5546. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5547. //Baby 3-1 Jobs
  5548. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5549. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5550. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5551. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5552. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5553. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5554. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5555. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5556. //Baby 3-2 Jobs
  5557. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5558. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5559. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5560. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5561. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5562. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5563. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5564. //Doram Jobs
  5565. case MAPID_SUMMONER: return JOB_SUMMONER;
  5566. default:
  5567. return -1;
  5568. }
  5569. }
  5570. /*====================================================
  5571. * This function return the name of the job (by [Yor])
  5572. *----------------------------------------------------*/
  5573. const char* job_name(int class_)
  5574. {
  5575. switch (class_) {
  5576. case JOB_NOVICE:
  5577. case JOB_SWORDMAN:
  5578. case JOB_MAGE:
  5579. case JOB_ARCHER:
  5580. case JOB_ACOLYTE:
  5581. case JOB_MERCHANT:
  5582. case JOB_THIEF:
  5583. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5584. case JOB_KNIGHT:
  5585. case JOB_PRIEST:
  5586. case JOB_WIZARD:
  5587. case JOB_BLACKSMITH:
  5588. case JOB_HUNTER:
  5589. case JOB_ASSASSIN:
  5590. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5591. case JOB_KNIGHT2:
  5592. return msg_txt(NULL,557);
  5593. case JOB_CRUSADER:
  5594. case JOB_MONK:
  5595. case JOB_SAGE:
  5596. case JOB_ROGUE:
  5597. case JOB_ALCHEMIST:
  5598. case JOB_BARD:
  5599. case JOB_DANCER:
  5600. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5601. case JOB_CRUSADER2:
  5602. return msg_txt(NULL,563);
  5603. case JOB_WEDDING:
  5604. case JOB_SUPER_NOVICE:
  5605. case JOB_GUNSLINGER:
  5606. case JOB_NINJA:
  5607. case JOB_XMAS:
  5608. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5609. case JOB_SUMMER:
  5610. case JOB_SUMMER2:
  5611. return msg_txt(NULL,621);
  5612. case JOB_HANBOK:
  5613. return msg_txt(NULL,694);
  5614. case JOB_OKTOBERFEST:
  5615. return msg_txt(NULL,696);
  5616. case JOB_NOVICE_HIGH:
  5617. case JOB_SWORDMAN_HIGH:
  5618. case JOB_MAGE_HIGH:
  5619. case JOB_ARCHER_HIGH:
  5620. case JOB_ACOLYTE_HIGH:
  5621. case JOB_MERCHANT_HIGH:
  5622. case JOB_THIEF_HIGH:
  5623. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5624. case JOB_LORD_KNIGHT:
  5625. case JOB_HIGH_PRIEST:
  5626. case JOB_HIGH_WIZARD:
  5627. case JOB_WHITESMITH:
  5628. case JOB_SNIPER:
  5629. case JOB_ASSASSIN_CROSS:
  5630. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5631. case JOB_LORD_KNIGHT2:
  5632. return msg_txt(NULL,582);
  5633. case JOB_PALADIN:
  5634. case JOB_CHAMPION:
  5635. case JOB_PROFESSOR:
  5636. case JOB_STALKER:
  5637. case JOB_CREATOR:
  5638. case JOB_CLOWN:
  5639. case JOB_GYPSY:
  5640. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5641. case JOB_PALADIN2:
  5642. return msg_txt(NULL,588);
  5643. case JOB_BABY:
  5644. case JOB_BABY_SWORDMAN:
  5645. case JOB_BABY_MAGE:
  5646. case JOB_BABY_ARCHER:
  5647. case JOB_BABY_ACOLYTE:
  5648. case JOB_BABY_MERCHANT:
  5649. case JOB_BABY_THIEF:
  5650. return msg_txt(NULL,595 - JOB_BABY + class_);
  5651. case JOB_BABY_KNIGHT:
  5652. case JOB_BABY_PRIEST:
  5653. case JOB_BABY_WIZARD:
  5654. case JOB_BABY_BLACKSMITH:
  5655. case JOB_BABY_HUNTER:
  5656. case JOB_BABY_ASSASSIN:
  5657. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5658. case JOB_BABY_KNIGHT2:
  5659. return msg_txt(NULL,602);
  5660. case JOB_BABY_CRUSADER:
  5661. case JOB_BABY_MONK:
  5662. case JOB_BABY_SAGE:
  5663. case JOB_BABY_ROGUE:
  5664. case JOB_BABY_ALCHEMIST:
  5665. case JOB_BABY_BARD:
  5666. case JOB_BABY_DANCER:
  5667. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5668. case JOB_BABY_CRUSADER2:
  5669. return msg_txt(NULL,608);
  5670. case JOB_SUPER_BABY:
  5671. return msg_txt(NULL,615);
  5672. case JOB_TAEKWON:
  5673. return msg_txt(NULL,616);
  5674. case JOB_STAR_GLADIATOR:
  5675. case JOB_STAR_GLADIATOR2:
  5676. return msg_txt(NULL,617);
  5677. case JOB_SOUL_LINKER:
  5678. return msg_txt(NULL,618);
  5679. case JOB_GANGSI:
  5680. case JOB_DEATH_KNIGHT:
  5681. case JOB_DARK_COLLECTOR:
  5682. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5683. case JOB_RUNE_KNIGHT:
  5684. case JOB_WARLOCK:
  5685. case JOB_RANGER:
  5686. case JOB_ARCH_BISHOP:
  5687. case JOB_MECHANIC:
  5688. case JOB_GUILLOTINE_CROSS:
  5689. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5690. case JOB_RUNE_KNIGHT_T:
  5691. case JOB_WARLOCK_T:
  5692. case JOB_RANGER_T:
  5693. case JOB_ARCH_BISHOP_T:
  5694. case JOB_MECHANIC_T:
  5695. case JOB_GUILLOTINE_CROSS_T:
  5696. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5697. case JOB_ROYAL_GUARD:
  5698. case JOB_SORCERER:
  5699. case JOB_MINSTREL:
  5700. case JOB_WANDERER:
  5701. case JOB_SURA:
  5702. case JOB_GENETIC:
  5703. case JOB_SHADOW_CHASER:
  5704. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5705. case JOB_ROYAL_GUARD_T:
  5706. case JOB_SORCERER_T:
  5707. case JOB_MINSTREL_T:
  5708. case JOB_WANDERER_T:
  5709. case JOB_SURA_T:
  5710. case JOB_GENETIC_T:
  5711. case JOB_SHADOW_CHASER_T:
  5712. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5713. case JOB_RUNE_KNIGHT2:
  5714. case JOB_RUNE_KNIGHT_T2:
  5715. return msg_txt(NULL,625);
  5716. case JOB_ROYAL_GUARD2:
  5717. case JOB_ROYAL_GUARD_T2:
  5718. return msg_txt(NULL,631);
  5719. case JOB_RANGER2:
  5720. case JOB_RANGER_T2:
  5721. return msg_txt(NULL,627);
  5722. case JOB_MECHANIC2:
  5723. case JOB_MECHANIC_T2:
  5724. return msg_txt(NULL,629);
  5725. case JOB_BABY_RUNE:
  5726. case JOB_BABY_WARLOCK:
  5727. case JOB_BABY_RANGER:
  5728. case JOB_BABY_BISHOP:
  5729. case JOB_BABY_MECHANIC:
  5730. case JOB_BABY_CROSS:
  5731. case JOB_BABY_GUARD:
  5732. case JOB_BABY_SORCERER:
  5733. case JOB_BABY_MINSTREL:
  5734. case JOB_BABY_WANDERER:
  5735. case JOB_BABY_SURA:
  5736. case JOB_BABY_GENETIC:
  5737. case JOB_BABY_CHASER:
  5738. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5739. case JOB_BABY_RUNE2:
  5740. return msg_txt(NULL,638);
  5741. case JOB_BABY_GUARD2:
  5742. return msg_txt(NULL,644);
  5743. case JOB_BABY_RANGER2:
  5744. return msg_txt(NULL,640);
  5745. case JOB_BABY_MECHANIC2:
  5746. return msg_txt(NULL,642);
  5747. case JOB_SUPER_NOVICE_E:
  5748. case JOB_SUPER_BABY_E:
  5749. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5750. case JOB_KAGEROU:
  5751. case JOB_OBORO:
  5752. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5753. case JOB_REBELLION:
  5754. return msg_txt(NULL,695);
  5755. case JOB_SUMMONER:
  5756. return msg_txt(NULL,697);
  5757. case JOB_BABY_SUMMONER:
  5758. return msg_txt(NULL,698);
  5759. case JOB_BABY_NINJA:
  5760. return msg_txt(NULL,699);
  5761. case JOB_BABY_KAGEROU:
  5762. case JOB_BABY_OBORO:
  5763. case JOB_BABY_TAEKWON:
  5764. case JOB_BABY_STAR_GLADIATOR:
  5765. case JOB_BABY_SOUL_LINKER:
  5766. case JOB_BABY_GUNSLINGER:
  5767. case JOB_BABY_REBELLION:
  5768. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5769. case JOB_BABY_STAR_GLADIATOR2:
  5770. return msg_txt(NULL,756);
  5771. case JOB_STAR_EMPEROR:
  5772. case JOB_SOUL_REAPER:
  5773. case JOB_BABY_STAR_EMPEROR:
  5774. case JOB_BABY_SOUL_REAPER:
  5775. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5776. case JOB_STAR_EMPEROR2:
  5777. return msg_txt(NULL,782);
  5778. case JOB_BABY_STAR_EMPEROR2:
  5779. return msg_txt(NULL,784);
  5780. default:
  5781. return msg_txt(NULL,655);
  5782. }
  5783. }
  5784. /*====================================================
  5785. * Timered function to make id follow a target.
  5786. * @id = bl.id (player only atm)
  5787. * target is define in sd->followtarget (bl.id)
  5788. * used by pc_follow
  5789. *----------------------------------------------------*/
  5790. TIMER_FUNC(pc_follow_timer){
  5791. struct map_session_data *sd;
  5792. struct block_list *tbl;
  5793. sd = map_id2sd(id);
  5794. nullpo_ret(sd);
  5795. if (sd->followtimer != tid){
  5796. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5797. sd->followtimer = INVALID_TIMER;
  5798. return 0;
  5799. }
  5800. sd->followtimer = INVALID_TIMER;
  5801. tbl = map_id2bl(sd->followtarget);
  5802. if (tbl == NULL || pc_isdead(sd))
  5803. {
  5804. pc_stop_following(sd);
  5805. return 0;
  5806. }
  5807. // either player or target is currently detached from map blocks (could be teleporting),
  5808. // but still connected to this map, so we'll just increment the timer and check back later
  5809. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5810. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5811. {
  5812. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5813. if (!check_distance_bl(&sd->bl, tbl, 5))
  5814. unit_walktobl(&sd->bl, tbl, 5, 0);
  5815. } else
  5816. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5817. }
  5818. sd->followtimer = add_timer(
  5819. tick + 1000, // increase time a bit to loosen up map's load
  5820. pc_follow_timer, sd->bl.id, 0);
  5821. return 0;
  5822. }
  5823. int pc_stop_following (struct map_session_data *sd)
  5824. {
  5825. nullpo_ret(sd);
  5826. if (sd->followtimer != INVALID_TIMER) {
  5827. delete_timer(sd->followtimer,pc_follow_timer);
  5828. sd->followtimer = INVALID_TIMER;
  5829. }
  5830. sd->followtarget = -1;
  5831. sd->ud.target_to = 0;
  5832. unit_stop_walking(&sd->bl, 1);
  5833. return 0;
  5834. }
  5835. int pc_follow(struct map_session_data *sd,int target_id)
  5836. {
  5837. struct block_list *bl = map_id2bl(target_id);
  5838. if (bl == NULL /*|| bl->type != BL_PC*/)
  5839. return 1;
  5840. if (sd->followtimer != INVALID_TIMER)
  5841. pc_stop_following(sd);
  5842. sd->followtarget = target_id;
  5843. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5844. return 0;
  5845. }
  5846. int pc_checkbaselevelup(struct map_session_data *sd) {
  5847. unsigned int next = pc_nextbaseexp(sd);
  5848. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5849. return 0;
  5850. do {
  5851. sd->status.base_exp -= next;
  5852. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5853. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5854. sd->status.base_exp = next-1;
  5855. next = pc_gets_status_point(sd->status.base_level);
  5856. sd->status.base_level++;
  5857. sd->status.status_point += next;
  5858. if( pc_is_maxbaselv(sd) ){
  5859. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5860. break;
  5861. }
  5862. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5863. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5864. status_calc_pet(sd->pd,SCO_NONE);
  5865. clif_updatestatus(sd,SP_STATUSPOINT);
  5866. clif_updatestatus(sd,SP_BASELEVEL);
  5867. clif_updatestatus(sd,SP_BASEEXP);
  5868. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5869. status_calc_pc(sd,SCO_FORCE);
  5870. status_percent_heal(&sd->bl,100,100);
  5871. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5872. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5873. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5874. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5875. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5876. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5877. if (sd->state.snovice_dead_flag)
  5878. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5879. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5880. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5881. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5882. }
  5883. clif_misceffect(&sd->bl,0);
  5884. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5885. if(sd->status.party_id)
  5886. party_send_levelup(sd);
  5887. pc_baselevelchanged(sd);
  5888. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5889. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5890. return 1;
  5891. }
  5892. void pc_baselevelchanged(struct map_session_data *sd) {
  5893. uint8 i;
  5894. for( i = 0; i < EQI_MAX; i++ ) {
  5895. if( sd->equip_index[i] >= 0 ) {
  5896. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5897. pc_unequipitem(sd, sd->equip_index[i], 3);
  5898. }
  5899. }
  5900. pc_show_questinfo(sd);
  5901. }
  5902. int pc_checkjoblevelup(struct map_session_data *sd)
  5903. {
  5904. unsigned int next = pc_nextjobexp(sd);
  5905. nullpo_ret(sd);
  5906. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5907. return 0;
  5908. do {
  5909. sd->status.job_exp -= next;
  5910. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5911. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5912. sd->status.job_exp = next-1;
  5913. sd->status.job_level ++;
  5914. sd->status.skill_point ++;
  5915. if( pc_is_maxjoblv(sd) ){
  5916. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5917. break;
  5918. }
  5919. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5920. clif_updatestatus(sd,SP_JOBLEVEL);
  5921. clif_updatestatus(sd,SP_JOBEXP);
  5922. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5923. clif_updatestatus(sd,SP_SKILLPOINT);
  5924. status_calc_pc(sd,SCO_FORCE);
  5925. clif_misceffect(&sd->bl,1);
  5926. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5927. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5928. npc_script_event(sd, NPCE_JOBLVUP);
  5929. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5930. return 1;
  5931. }
  5932. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5933. * @param sd Player
  5934. * @param base_exp Base EXP before peronal bonuses
  5935. * @param job_exp Job EXP before peronal bonuses
  5936. * @param src Block list that affecting the exp calculation
  5937. */
  5938. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5939. {
  5940. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5941. if (src) {
  5942. struct status_data *status = status_get_status_data(src);
  5943. if( sd->expaddrace[status->race] )
  5944. bonus += sd->expaddrace[status->race];
  5945. if( sd->expaddrace[RC_ALL] )
  5946. bonus += sd->expaddrace[RC_ALL];
  5947. if( sd->expaddclass[status->class_] )
  5948. bonus += sd->expaddclass[status->class_];
  5949. if( sd->expaddclass[CLASS_ALL] )
  5950. bonus += sd->expaddclass[CLASS_ALL];
  5951. if (battle_config.pk_mode &&
  5952. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5953. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5954. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5955. vip_bonus_base = battle_config.vip_base_exp_increase;
  5956. vip_bonus_job = battle_config.vip_job_exp_increase;
  5957. }
  5958. }
  5959. // Give EXPBOOST for quests even if src is NULL.
  5960. if (sd->sc.data[SC_EXPBOOST]) {
  5961. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5962. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5963. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5964. }
  5965. if (*base_exp) {
  5966. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5967. *base_exp = cap_value(exp, 1, UINT_MAX);
  5968. }
  5969. // Give JEXPBOOST for quests even if src is NULL.
  5970. if (sd->sc.data[SC_JEXPBOOST])
  5971. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5972. if (*job_exp) {
  5973. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5974. *job_exp = cap_value(exp, 1, UINT_MAX);
  5975. }
  5976. return;
  5977. }
  5978. /**
  5979. * Show EXP gained by player in percentage by @showexp
  5980. * @param sd Player
  5981. * @param base_exp Base EXP gained/loss
  5982. * @param next_base_exp Base EXP needed for next base level
  5983. * @param job_exp Job EXP gained/loss
  5984. * @param next_job_exp Job EXP needed for next job level
  5985. * @param lost True:EXP penalty, lose EXP
  5986. **/
  5987. 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) {
  5988. char output[CHAT_SIZE_MAX];
  5989. nullpo_retv(sd);
  5990. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5991. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5992. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5993. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5994. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5995. }
  5996. /**
  5997. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5998. * @param sd Player
  5999. * @param src EXP source
  6000. * @param base_exp Base EXP gained
  6001. * @param base_exp Job EXP gained
  6002. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6003. * @return
  6004. **/
  6005. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6006. {
  6007. unsigned int nextb = 0, nextj = 0;
  6008. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6009. nullpo_retv(sd);
  6010. if(sd->bl.prev == NULL || pc_isdead(sd))
  6011. return;
  6012. if (!(exp_flag&2)) {
  6013. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6014. return; // no exp on pvp maps
  6015. if (sd->status.guild_id>0)
  6016. base_exp -= guild_payexp(sd,base_exp);
  6017. }
  6018. flag = ((base_exp) ? 1 : 0) |
  6019. ((job_exp) ? 2 : 0) |
  6020. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6021. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6022. if (!(exp_flag&2))
  6023. pc_calcexp(sd, &base_exp, &job_exp, src);
  6024. nextb = pc_nextbaseexp(sd);
  6025. nextj = pc_nextjobexp(sd);
  6026. if (flag&4){
  6027. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6028. base_exp = 0;
  6029. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6030. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6031. }
  6032. if (flag&8){
  6033. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6034. job_exp = 0;
  6035. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6036. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6037. }
  6038. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6039. //Note that this value should never be greater than the original
  6040. //therefore no overflow checks are needed. [Skotlex]
  6041. if (nextb > 0) {
  6042. float nextbp = (float) base_exp / (float) nextb;
  6043. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6044. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6045. }
  6046. if (nextj > 0) {
  6047. float nextjp = (float) job_exp / (float) nextj;
  6048. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6049. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6050. }
  6051. }
  6052. // Give EXP for Base Level
  6053. if (base_exp) {
  6054. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6055. sd->status.base_exp = UINT32_MAX;
  6056. else
  6057. sd->status.base_exp += base_exp;
  6058. if (!pc_checkbaselevelup(sd))
  6059. clif_updatestatus(sd,SP_BASEEXP);
  6060. }
  6061. // Give EXP for Job Level
  6062. if (job_exp) {
  6063. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6064. sd->status.job_exp = UINT32_MAX;
  6065. else
  6066. sd->status.job_exp += job_exp;
  6067. if (!pc_checkjoblevelup(sd))
  6068. clif_updatestatus(sd,SP_JOBEXP);
  6069. }
  6070. if (flag&1)
  6071. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6072. if (flag&2)
  6073. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6074. if (sd->state.showexp && (base_exp || job_exp))
  6075. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6076. }
  6077. /**
  6078. * Lost Base/Job EXP from a player
  6079. * @param sd Player
  6080. * @param base_exp Base EXP lost
  6081. * @param job_exp Job EXP lost
  6082. **/
  6083. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6084. nullpo_retv(sd);
  6085. if (base_exp) {
  6086. base_exp = u32min(sd->status.base_exp, base_exp);
  6087. sd->status.base_exp -= base_exp;
  6088. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6089. clif_updatestatus(sd, SP_BASEEXP);
  6090. }
  6091. if (job_exp) {
  6092. job_exp = u32min(sd->status.job_exp, job_exp);
  6093. sd->status.job_exp -= job_exp;
  6094. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6095. clif_updatestatus(sd, SP_JOBEXP);
  6096. }
  6097. if (sd->state.showexp && (base_exp || job_exp))
  6098. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6099. }
  6100. /**
  6101. * Returns max base level for this character's class.
  6102. * @param class_: Player's class
  6103. * @return Max Base Level
  6104. */
  6105. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6106. return job_info[pc_class2idx(class_)].max_level[0];
  6107. }
  6108. /**
  6109. * Returns max base level for this character.
  6110. * @param sd Player
  6111. * @return Max Base Level
  6112. **/
  6113. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6114. return pc_class_maxbaselv(sd->status.class_);
  6115. }
  6116. /**
  6117. * Returns max job level for this character's class.
  6118. * @param class_: Player's class
  6119. * @return Max Job Level
  6120. */
  6121. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6122. return job_info[pc_class2idx(class_)].max_level[1];
  6123. }
  6124. /**
  6125. * Returns max job level for this character.
  6126. * @param sd Player
  6127. * @return Max Job Level
  6128. **/
  6129. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6130. return pc_class_maxjoblv(sd->status.class_);
  6131. }
  6132. /**
  6133. * Check if player is reached max base level
  6134. * @param sd
  6135. * @return True if reached max level
  6136. **/
  6137. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6138. nullpo_retr(false, sd);
  6139. return (sd->status.base_level >= pc_maxbaselv(sd));
  6140. }
  6141. /**
  6142. * Check if player is reached max base level
  6143. * @param sd
  6144. * @return True if reached max level
  6145. **/
  6146. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6147. nullpo_retr(false, sd);
  6148. return (sd->status.job_level >= pc_maxjoblv(sd));
  6149. }
  6150. /**
  6151. * Base exp needed for player to level up.
  6152. * @param sd
  6153. * @return Base EXP needed for next base level
  6154. **/
  6155. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6156. nullpo_ret(sd);
  6157. if (sd->status.base_level == 0) // Is this something that possible?
  6158. return 0;
  6159. if (pc_is_maxbaselv(sd))
  6160. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6161. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6162. }
  6163. /**
  6164. * Job exp needed for player to level up.
  6165. * @param sd
  6166. * @return Job EXP needed for next job level
  6167. **/
  6168. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6169. nullpo_ret(sd);
  6170. if (sd->status.job_level == 0) // Is this something that possible?
  6171. return 0;
  6172. if (pc_is_maxjoblv(sd))
  6173. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6174. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6175. }
  6176. /// Returns the value of the specified stat.
  6177. static int pc_getstat(struct map_session_data* sd, int type)
  6178. {
  6179. nullpo_retr(-1, sd);
  6180. switch( type ) {
  6181. case SP_STR: return sd->status.str;
  6182. case SP_AGI: return sd->status.agi;
  6183. case SP_VIT: return sd->status.vit;
  6184. case SP_INT: return sd->status.int_;
  6185. case SP_DEX: return sd->status.dex;
  6186. case SP_LUK: return sd->status.luk;
  6187. default:
  6188. return -1;
  6189. }
  6190. }
  6191. /// Sets the specified stat to the specified value.
  6192. /// Returns the new value.
  6193. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6194. {
  6195. nullpo_retr(-1, sd);
  6196. switch( type ) {
  6197. case SP_STR: sd->status.str = val; break;
  6198. case SP_AGI: sd->status.agi = val; break;
  6199. case SP_VIT: sd->status.vit = val; break;
  6200. case SP_INT: sd->status.int_ = val; break;
  6201. case SP_DEX: sd->status.dex = val; break;
  6202. case SP_LUK: sd->status.luk = val; break;
  6203. default:
  6204. return -1;
  6205. }
  6206. return val;
  6207. }
  6208. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6209. int pc_gets_status_point(int level)
  6210. {
  6211. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6212. return (statp[level+1] - statp[level]);
  6213. else //Default increase
  6214. return ((level+15) / 5);
  6215. }
  6216. #ifdef RENEWAL_STAT
  6217. /// Renewal status point cost formula
  6218. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6219. #else
  6220. /// Pre-Renewal status point cost formula
  6221. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6222. #endif
  6223. /// Returns the number of stat points needed to change the specified stat by val.
  6224. /// If val is negative, returns the number of stat points that would be needed to
  6225. /// raise the specified stat from (current value - val) to current value.
  6226. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6227. {
  6228. int low, high, sp = 0, max = 0;
  6229. if ( val == 0 )
  6230. return 0;
  6231. low = pc_getstat(sd,type);
  6232. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6233. if ( low >= max && val > 0 )
  6234. return 0; // Official servers show '0' when max is reached
  6235. high = low + val;
  6236. if ( val < 0 )
  6237. SWAP(low, high);
  6238. for ( ; low < high; low++ )
  6239. sp += PC_STATUS_POINT_COST(low);
  6240. return sp;
  6241. }
  6242. /**
  6243. * Returns the value the specified stat can be increased by with the current
  6244. * amount of available status points for the current character's class.
  6245. *
  6246. * @param sd The target character.
  6247. * @param type Stat to verify.
  6248. * @return Maximum value the stat could grow by.
  6249. */
  6250. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6251. {
  6252. int base, final_val, status_points, max_param;
  6253. nullpo_ret(sd);
  6254. base = final_val = pc_getstat(sd, type);
  6255. status_points = sd->status.status_point;
  6256. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6257. while (final_val <= max_param && status_points >= 0) {
  6258. status_points -= PC_STATUS_POINT_COST(final_val);
  6259. final_val++;
  6260. }
  6261. final_val--;
  6262. return (final_val > base ? final_val-base : 0);
  6263. }
  6264. /**
  6265. * Raises a stat by the specified amount.
  6266. *
  6267. * Obeys max_parameter limits.
  6268. * Subtracts status points according to the cost of the increased stat points.
  6269. *
  6270. * @param sd The target character.
  6271. * @param type The stat to change (see enum _sp)
  6272. * @param increase The stat increase (strictly positive) amount.
  6273. * @retval true if the stat was increased by any amount.
  6274. * @retval false if there were no changes.
  6275. */
  6276. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6277. {
  6278. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6279. nullpo_ret(sd);
  6280. // check conditions
  6281. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6282. clif_statusupack(sd, type, 0, 0);
  6283. return false;
  6284. }
  6285. // check limits
  6286. current = pc_getstat(sd, type);
  6287. max_increase = pc_maxparameterincrease(sd, type);
  6288. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6289. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6290. clif_statusupack(sd, type, 0, 0);
  6291. return false;
  6292. }
  6293. // check status points
  6294. needed_points = pc_need_status_point(sd, type, increase);
  6295. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6296. clif_statusupack(sd, type, 0, 0);
  6297. return false;
  6298. }
  6299. // set new values
  6300. final_value = pc_setstat(sd, type, current + increase);
  6301. sd->status.status_point -= needed_points;
  6302. status_calc_pc(sd,SCO_NONE);
  6303. // update increase cost indicator
  6304. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6305. // update statpoint count
  6306. clif_updatestatus(sd, SP_STATUSPOINT);
  6307. // update stat value
  6308. clif_statusupack(sd, type, 1, final_value); // required
  6309. if( final_value > 255 )
  6310. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6311. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6312. return true;
  6313. }
  6314. /**
  6315. * Raises a stat by the specified amount.
  6316. *
  6317. * Obeys max_parameter limits.
  6318. * Does not subtract status points for the cost of the modified stat points.
  6319. *
  6320. * @param sd The target character.
  6321. * @param type The stat to change (see enum _sp)
  6322. * @param val The stat increase (or decrease) amount.
  6323. * @return the stat increase amount.
  6324. * @retval 0 if no changes were made.
  6325. */
  6326. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6327. {
  6328. int max, need;
  6329. nullpo_ret(sd);
  6330. if( type < SP_STR || type > SP_LUK )
  6331. {
  6332. clif_statusupack(sd,type,0,0);
  6333. return 0;
  6334. }
  6335. need = pc_need_status_point(sd,type,1);
  6336. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6337. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6338. status_calc_pc(sd,SCO_NONE);
  6339. // update increase cost indicator
  6340. if( need != pc_need_status_point(sd,type,1) )
  6341. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6342. // update stat value
  6343. clif_statusupack(sd,type,1,val); // required
  6344. if( val > 255 )
  6345. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6346. return val;
  6347. }
  6348. /*==========================================
  6349. * Update skill_lv for player sd
  6350. * Skill point allocation
  6351. *------------------------------------------*/
  6352. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6353. {
  6354. uint16 idx = skill_get_index(skill_id);
  6355. nullpo_retv(sd);
  6356. if (!idx) {
  6357. if (skill_id)
  6358. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6359. return;
  6360. }
  6361. // Level up guild skill
  6362. if (SKILL_CHK_GUILD(skill_id)) {
  6363. guild_skillup(sd, skill_id);
  6364. return;
  6365. }
  6366. // Level up homunculus skill
  6367. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6368. hom_skillup(sd->hd, skill_id);
  6369. return;
  6370. }
  6371. else {
  6372. if( sd->status.skill_point > 0 &&
  6373. sd->status.skill[idx].id &&
  6374. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6375. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6376. {
  6377. int lv, range, upgradable;
  6378. sd->status.skill[idx].lv++;
  6379. sd->status.skill_point--;
  6380. if( !skill_get_inf(skill_id) )
  6381. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6382. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6383. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6384. else
  6385. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6386. lv = sd->status.skill[idx].lv;
  6387. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6388. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6389. clif_skillup(sd,skill_id,lv,range,upgradable);
  6390. clif_updatestatus(sd,SP_SKILLPOINT);
  6391. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6392. clif_updatestatus(sd,SP_CARTINFO);
  6393. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6394. clif_skillinfoblock(sd);
  6395. }
  6396. //else
  6397. // 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);
  6398. }
  6399. }
  6400. /*==========================================
  6401. * /allskill
  6402. *------------------------------------------*/
  6403. int pc_allskillup(struct map_session_data *sd)
  6404. {
  6405. int i;
  6406. nullpo_ret(sd);
  6407. for (i = 0; i < MAX_SKILL; i++) {
  6408. 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) {
  6409. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6410. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6411. if (sd->status.skill[i].lv == 0)
  6412. sd->status.skill[i].id = 0;
  6413. }
  6414. }
  6415. if (!pc_grant_allskills(sd, true)) {
  6416. uint16 sk_id;
  6417. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6418. int inf2 = 0;
  6419. uint16 sk_idx = 0;
  6420. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6421. continue;
  6422. inf2 = skill_get_inf2(sk_id);
  6423. if (
  6424. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6425. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6426. sk_id == SG_DEVIL
  6427. )
  6428. continue; //Cannot be learned normally.
  6429. sd->status.skill[sk_idx].id = sk_id;
  6430. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6431. }
  6432. }
  6433. status_calc_pc(sd,SCO_NONE);
  6434. //Required because if you could level up all skills previously,
  6435. //the update will not be sent as only the lv variable changes.
  6436. clif_skillinfoblock(sd);
  6437. return 0;
  6438. }
  6439. /*==========================================
  6440. * /resetlvl
  6441. *------------------------------------------*/
  6442. int pc_resetlvl(struct map_session_data* sd,int type)
  6443. {
  6444. int i;
  6445. nullpo_ret(sd);
  6446. if (type != 3) //Also reset skills
  6447. pc_resetskill(sd, 0);
  6448. if(type == 1){
  6449. sd->status.skill_point=0;
  6450. sd->status.base_level=1;
  6451. sd->status.job_level=1;
  6452. sd->status.base_exp=0;
  6453. sd->status.job_exp=0;
  6454. if(sd->sc.option !=0)
  6455. sd->sc.option = 0;
  6456. sd->status.str=1;
  6457. sd->status.agi=1;
  6458. sd->status.vit=1;
  6459. sd->status.int_=1;
  6460. sd->status.dex=1;
  6461. sd->status.luk=1;
  6462. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6463. sd->status.status_point=100; // not 88 [celest]
  6464. // give platinum skills upon changing
  6465. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6466. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6467. }
  6468. }
  6469. if(type == 2){
  6470. sd->status.skill_point=0;
  6471. sd->status.base_level=1;
  6472. sd->status.job_level=1;
  6473. sd->status.base_exp=0;
  6474. sd->status.job_exp=0;
  6475. }
  6476. if(type == 3){
  6477. sd->status.base_level=1;
  6478. sd->status.base_exp=0;
  6479. }
  6480. if(type == 4){
  6481. sd->status.job_level=1;
  6482. sd->status.job_exp=0;
  6483. }
  6484. clif_updatestatus(sd,SP_STATUSPOINT);
  6485. clif_updatestatus(sd,SP_STR);
  6486. clif_updatestatus(sd,SP_AGI);
  6487. clif_updatestatus(sd,SP_VIT);
  6488. clif_updatestatus(sd,SP_INT);
  6489. clif_updatestatus(sd,SP_DEX);
  6490. clif_updatestatus(sd,SP_LUK);
  6491. clif_updatestatus(sd,SP_BASELEVEL);
  6492. clif_updatestatus(sd,SP_JOBLEVEL);
  6493. clif_updatestatus(sd,SP_STATUSPOINT);
  6494. clif_updatestatus(sd,SP_BASEEXP);
  6495. clif_updatestatus(sd,SP_JOBEXP);
  6496. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6497. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6498. clif_updatestatus(sd,SP_SKILLPOINT);
  6499. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6500. clif_updatestatus(sd,SP_UAGI);
  6501. clif_updatestatus(sd,SP_UVIT);
  6502. clif_updatestatus(sd,SP_UINT);
  6503. clif_updatestatus(sd,SP_UDEX);
  6504. clif_updatestatus(sd,SP_ULUK); // End Addition
  6505. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6506. if(sd->equip_index[i] >= 0)
  6507. if(pc_isequip(sd,sd->equip_index[i]))
  6508. pc_unequipitem(sd,sd->equip_index[i],2);
  6509. }
  6510. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6511. party_send_levelup(sd);
  6512. status_calc_pc(sd, SCO_FORCE);
  6513. clif_skillinfoblock(sd);
  6514. return 0;
  6515. }
  6516. /*==========================================
  6517. * /resetstate
  6518. *------------------------------------------*/
  6519. int pc_resetstate(struct map_session_data* sd)
  6520. {
  6521. nullpo_ret(sd);
  6522. if (battle_config.use_statpoint_table)
  6523. { // New statpoint table used here - Dexity
  6524. if (sd->status.base_level > MAX_LEVEL)
  6525. { //statp[] goes out of bounds, can't reset!
  6526. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6527. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6528. return 0;
  6529. }
  6530. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6531. }
  6532. else
  6533. {
  6534. int add=0;
  6535. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6536. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6537. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6538. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6539. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6540. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6541. sd->status.status_point+=add;
  6542. }
  6543. pc_setstat(sd, SP_STR, 1);
  6544. pc_setstat(sd, SP_AGI, 1);
  6545. pc_setstat(sd, SP_VIT, 1);
  6546. pc_setstat(sd, SP_INT, 1);
  6547. pc_setstat(sd, SP_DEX, 1);
  6548. pc_setstat(sd, SP_LUK, 1);
  6549. clif_updatestatus(sd,SP_STR);
  6550. clif_updatestatus(sd,SP_AGI);
  6551. clif_updatestatus(sd,SP_VIT);
  6552. clif_updatestatus(sd,SP_INT);
  6553. clif_updatestatus(sd,SP_DEX);
  6554. clif_updatestatus(sd,SP_LUK);
  6555. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6556. clif_updatestatus(sd,SP_UAGI);
  6557. clif_updatestatus(sd,SP_UVIT);
  6558. clif_updatestatus(sd,SP_UINT);
  6559. clif_updatestatus(sd,SP_UDEX);
  6560. clif_updatestatus(sd,SP_ULUK); // End Addition
  6561. clif_updatestatus(sd,SP_STATUSPOINT);
  6562. if( sd->mission_mobid ) { //bugreport:2200
  6563. sd->mission_mobid = 0;
  6564. sd->mission_count = 0;
  6565. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6566. }
  6567. status_calc_pc(sd, SCO_NONE);
  6568. return 1;
  6569. }
  6570. /*==========================================
  6571. * /resetskill
  6572. * if flag&1, perform block resync and status_calc call.
  6573. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6574. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6575. *------------------------------------------*/
  6576. int pc_resetskill(struct map_session_data* sd, int flag)
  6577. {
  6578. int i, skill_point=0;
  6579. nullpo_ret(sd);
  6580. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6581. return 0;
  6582. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6583. /**
  6584. * 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)
  6585. **/
  6586. if( pc_is_taekwon_ranker(sd) )
  6587. return 0;
  6588. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6589. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6590. i = sd->sc.option;
  6591. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6592. i &= ~OPTION_RIDING;
  6593. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6594. i &= ~OPTION_FALCON;
  6595. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6596. i &= ~OPTION_DRAGON;
  6597. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6598. i &= ~OPTION_WUG;
  6599. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6600. i &= ~OPTION_WUGRIDER;
  6601. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6602. i &= ~OPTION_MADOGEAR;
  6603. #ifndef NEW_CARTS
  6604. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6605. i &= ~OPTION_CART;
  6606. #else
  6607. if( sd->sc.data[SC_PUSH_CART] )
  6608. pc_setcart(sd, 0);
  6609. #endif
  6610. if( i != sd->sc.option )
  6611. pc_setoption(sd, i);
  6612. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6613. hom_vaporize(sd, HOM_ST_ACTIVE);
  6614. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6615. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6616. }
  6617. for( i = 1; i < MAX_SKILL; i++ )
  6618. {
  6619. uint8 lv = sd->status.skill[i].lv;
  6620. int inf2;
  6621. uint16 skill_id = skill_idx2id(i);
  6622. if (lv == 0 || skill_id == 0)
  6623. continue;
  6624. inf2 = skill_get_inf2(skill_id);
  6625. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6626. continue;
  6627. // Don't reset trick dead if not a novice/baby
  6628. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6629. {
  6630. sd->status.skill[i].lv = 0;
  6631. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6632. continue;
  6633. }
  6634. // do not reset basic skill
  6635. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6636. continue;
  6637. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6638. continue;
  6639. if( flag&4 && !skill_ischangesex(skill_id) )
  6640. continue;
  6641. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6642. { //Only handle quest skills in a special way when you can't learn them manually
  6643. if( battle_config.quest_skill_reset && !(flag&2) )
  6644. { //Wipe them
  6645. sd->status.skill[i].lv = 0;
  6646. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6647. }
  6648. continue;
  6649. }
  6650. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6651. skill_point += lv;
  6652. else
  6653. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6654. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6655. if( !(flag&2) )
  6656. {// reset
  6657. sd->status.skill[i].lv = 0;
  6658. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6659. }
  6660. }
  6661. if( flag&2 || !skill_point ) return skill_point;
  6662. sd->status.skill_point += skill_point;
  6663. if (flag&1) {
  6664. clif_updatestatus(sd,SP_SKILLPOINT);
  6665. clif_skillinfoblock(sd);
  6666. status_calc_pc(sd, SCO_FORCE);
  6667. }
  6668. return skill_point;
  6669. }
  6670. /*==========================================
  6671. * /resetfeel [Komurka]
  6672. *------------------------------------------*/
  6673. int pc_resetfeel(struct map_session_data* sd)
  6674. {
  6675. int i;
  6676. nullpo_ret(sd);
  6677. for (i=0; i<MAX_PC_FEELHATE; i++)
  6678. {
  6679. sd->feel_map[i].m = -1;
  6680. sd->feel_map[i].index = 0;
  6681. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6682. }
  6683. return 0;
  6684. }
  6685. int pc_resethate(struct map_session_data* sd)
  6686. {
  6687. int i;
  6688. nullpo_ret(sd);
  6689. for (i=0; i<3; i++)
  6690. {
  6691. sd->hate_mob[i] = -1;
  6692. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6693. }
  6694. return 0;
  6695. }
  6696. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6697. {
  6698. int i, bonus = 0;
  6699. nullpo_ret(sd);
  6700. skill_id = skill_dummy2skill_id(skill_id);
  6701. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6702. if( i < ARRAYLENGTH(sd->skillatk) )
  6703. bonus = sd->skillatk[i].val;
  6704. return bonus;
  6705. }
  6706. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6707. {
  6708. int i, bonus = 0;
  6709. nullpo_ret(sd);
  6710. skill_id = skill_dummy2skill_id(skill_id);
  6711. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6712. if( i < ARRAYLENGTH(sd->subskill) )
  6713. bonus = sd->subskill[i].val;
  6714. return bonus;
  6715. }
  6716. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6717. int i, bonus = sd->bonus.add_heal_rate;
  6718. skill_id = skill_dummy2skill_id(skill_id);
  6719. if( bonus ) {
  6720. switch( skill_id ) {
  6721. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6722. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6723. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6724. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6725. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6726. }
  6727. }
  6728. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6729. if( i < ARRAYLENGTH(sd->skillheal) )
  6730. bonus += sd->skillheal[i].val;
  6731. return bonus;
  6732. }
  6733. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6734. int i, bonus = sd->bonus.add_heal2_rate;
  6735. skill_id = skill_dummy2skill_id(skill_id);
  6736. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6737. if( i < ARRAYLENGTH(sd->skillheal2) )
  6738. bonus += sd->skillheal2[i].val;
  6739. return bonus;
  6740. }
  6741. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6742. {
  6743. if( !pc_isdead(sd) )
  6744. return; // not applicable
  6745. if( sd->bg_id && bg_member_respawn(sd) )
  6746. return; // member revived by battleground
  6747. pc_setstand(sd, true);
  6748. pc_setrestartvalue(sd,3);
  6749. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6750. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6751. }
  6752. static TIMER_FUNC(pc_respawn_timer){
  6753. struct map_session_data *sd = map_id2sd(id);
  6754. if( sd != NULL )
  6755. {
  6756. sd->pvp_point=0;
  6757. pc_respawn(sd,CLR_OUTSIGHT);
  6758. }
  6759. return 0;
  6760. }
  6761. /*==========================================
  6762. * Invoked when a player has received damage
  6763. *------------------------------------------*/
  6764. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6765. {
  6766. if (sp) clif_updatestatus(sd,SP_SP);
  6767. if (hp) clif_updatestatus(sd,SP_HP);
  6768. else return;
  6769. if (!src)
  6770. return;
  6771. if( pc_issit(sd) ) {
  6772. pc_setstand(sd, true);
  6773. skill_sit(sd,0);
  6774. }
  6775. if (sd->progressbar.npc_id)
  6776. clif_progressbar_abort(sd);
  6777. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6778. pet_target_check(sd->pd,src,1);
  6779. if( sd->status.ele_id > 0 )
  6780. elemental_set_target(sd,src);
  6781. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6782. sd->canlog_tick = gettick();
  6783. }
  6784. TIMER_FUNC(pc_close_npc_timer){
  6785. TBL_PC *sd = map_id2sd(id);
  6786. if(sd) pc_close_npc(sd,data);
  6787. return 0;
  6788. }
  6789. /*
  6790. * Method to properly close npc for player and clear anything related
  6791. * @flag == 1 : produce close button
  6792. * @flag == 2 : directly close it
  6793. */
  6794. void pc_close_npc(struct map_session_data *sd,int flag)
  6795. {
  6796. nullpo_retv(sd);
  6797. if (sd->npc_id || sd->npc_shopid) {
  6798. if (sd->state.using_fake_npc) {
  6799. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6800. sd->state.using_fake_npc = 0;
  6801. }
  6802. if (sd->st) {
  6803. if(sd->st->state == RUN){ //wait ending code execution
  6804. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6805. return;
  6806. }
  6807. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6808. sd->st->mes_active = 0;
  6809. }
  6810. sd->state.menu_or_input = 0;
  6811. sd->npc_menu = 0;
  6812. sd->npc_shopid = 0;
  6813. #ifdef SECURE_NPCTIMEOUT
  6814. sd->npc_idle_timer = INVALID_TIMER;
  6815. #endif
  6816. clif_scriptclose(sd,sd->npc_id);
  6817. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6818. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6819. script_free_state(sd->st);
  6820. sd->st = NULL;
  6821. sd->npc_id = 0;
  6822. }
  6823. }
  6824. }
  6825. /*==========================================
  6826. * Invoked when a player has negative current hp
  6827. *------------------------------------------*/
  6828. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6829. {
  6830. int i=0,k=0;
  6831. unsigned int tick = gettick();
  6832. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6833. // Super Novices have no kill or die functions attached when saved by their angel
  6834. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6835. unsigned int exp = pc_nextbaseexp(sd);
  6836. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6837. sd->state.snovice_dead_flag = 1;
  6838. pc_setrestartvalue(sd,1);
  6839. status_percent_heal(&sd->bl, 100, 100);
  6840. clif_resurrection(&sd->bl, 1);
  6841. if(battle_config.pc_invincible_time)
  6842. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6843. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6844. if(map_flag_gvg2(sd->bl.m))
  6845. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6846. return 0;
  6847. }
  6848. }
  6849. for(k = 0; k < MAX_DEVOTION; k++) {
  6850. if (sd->devotion[k]){
  6851. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6852. if (devsd)
  6853. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6854. sd->devotion[k] = 0;
  6855. }
  6856. }
  6857. if(sd->shadowform_id) { //if we were target of shadowform
  6858. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6859. sd->shadowform_id = 0; //should be remove on status end anyway
  6860. }
  6861. if(sd->status.pet_id > 0 && sd->pd) {
  6862. struct pet_data *pd = sd->pd;
  6863. if( !map_getmapflag(sd->bl.m, MF_NOEXPPENALTY) ) {
  6864. pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
  6865. if( pd->pet.intimate < 0 )
  6866. pd->pet.intimate = 0;
  6867. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6868. }
  6869. if( sd->pd->target_id ) // Unlock all targets...
  6870. pet_unlocktarget(sd->pd);
  6871. }
  6872. if (hom_is_active(sd->hd) && battle_config.homunculus_auto_vapor && get_percentage(sd->hd->battle_status.hp, sd->hd->battle_status.max_hp) >= battle_config.homunculus_auto_vapor)
  6873. hom_vaporize(sd, HOM_ST_ACTIVE);
  6874. if( sd->md )
  6875. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6876. if( sd->ed )
  6877. elemental_delete(sd->ed);
  6878. // Leave duel if you die [LuzZza]
  6879. if(battle_config.duel_autoleave_when_die) {
  6880. if(sd->duel_group > 0)
  6881. duel_leave(sd->duel_group, sd);
  6882. if(sd->duel_invite > 0)
  6883. duel_reject(sd->duel_invite, sd);
  6884. }
  6885. pc_close_npc(sd,2); //close npc if we were using one
  6886. /* e.g. not killed thru pc_damage */
  6887. if( pc_issit(sd) ) {
  6888. clif_status_load(&sd->bl,EFST_SIT,0);
  6889. }
  6890. pc_setdead(sd);
  6891. clif_party_dead( sd );
  6892. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  6893. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6894. //Reset menu skills/item skills
  6895. if ((sd->skillitem) != 0)
  6896. sd->skillitem = sd->skillitemlv = 0;
  6897. if ((sd->menuskill_id) != 0)
  6898. sd->menuskill_id = sd->menuskill_val = 0;
  6899. //Reset ticks.
  6900. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6901. if ( sd->spiritball !=0 )
  6902. pc_delspiritball(sd,sd->spiritball,0);
  6903. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6904. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6905. if (src)
  6906. switch (src->type) {
  6907. case BL_MOB:
  6908. {
  6909. struct mob_data *md=(struct mob_data *)src;
  6910. if(md->target_id==sd->bl.id)
  6911. mob_unlocktarget(md,tick);
  6912. if(battle_config.mobs_level_up && md->status.hp &&
  6913. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6914. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6915. ) { // monster level up [Valaris]
  6916. clif_misceffect(&md->bl,0);
  6917. md->level++;
  6918. status_calc_mob(md, SCO_NONE);
  6919. status_percent_heal(src,10,0);
  6920. if( battle_config.show_mob_info&4 )
  6921. {// update name with new level
  6922. clif_name_area(&md->bl);
  6923. }
  6924. }
  6925. src = battle_get_master(src); // Maybe Player Summon
  6926. }
  6927. break;
  6928. case BL_PET: //Pass on to master...
  6929. case BL_HOM:
  6930. case BL_MER:
  6931. src = battle_get_master(src);
  6932. break;
  6933. }
  6934. if (src && src->type == BL_PC) {
  6935. struct map_session_data *ssd = (struct map_session_data *)src;
  6936. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6937. npc_script_event(ssd, NPCE_KILLPC);
  6938. if (battle_config.pk_mode&2) {
  6939. ssd->status.manner -= 5;
  6940. if(ssd->status.manner < 0)
  6941. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6942. #if 0
  6943. // PK/Karma system code (not enabled yet) [celest]
  6944. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6945. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6946. // karma going down = more 'good' / more honourable.
  6947. // The Karma System way...
  6948. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6949. sd->status.karma--;
  6950. ssd->status.karma--;
  6951. }
  6952. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6953. ssd->status.karma++;
  6954. // or the PK System way...
  6955. if (sd->status.karma > 0) // player killed is dishonourable?
  6956. ssd->status.karma--; // honour points earned
  6957. sd->status.karma++; // honour points lost
  6958. // To-do: Receive exp on certain occasions
  6959. #endif
  6960. }
  6961. }
  6962. if(battle_config.bone_drop==2
  6963. || (battle_config.bone_drop==1 && map_getmapflag(sd->bl.m, MF_PVP)))
  6964. {
  6965. struct item item_tmp;
  6966. memset(&item_tmp,0,sizeof(item_tmp));
  6967. item_tmp.nameid=ITEMID_SKULL_;
  6968. item_tmp.identify=1;
  6969. item_tmp.card[0]=CARD0_CREATE;
  6970. item_tmp.card[1]=0;
  6971. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6972. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6973. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6974. }
  6975. //Remove bonus_script when dead
  6976. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6977. // changed penalty options, added death by player if pk_mode [Valaris]
  6978. if(battle_config.death_penalty_type
  6979. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6980. && !map_getmapflag(sd->bl.m, MF_NOEXPPENALTY) && !map_flag_gvg2(sd->bl.m)
  6981. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6982. {
  6983. uint32 base_penalty = 0;
  6984. uint32 job_penalty = 0;
  6985. uint32 zeny_penalty = 0;
  6986. if (pc_isvip(sd)) { // EXP penalty for VIP
  6987. base_penalty = battle_config.vip_exp_penalty_base;
  6988. job_penalty = battle_config.vip_exp_penalty_job;
  6989. zeny_penalty = battle_config.vip_zeny_penalty;
  6990. } else {
  6991. base_penalty = battle_config.death_penalty_base;
  6992. job_penalty = battle_config.death_penalty_job;
  6993. zeny_penalty = battle_config.zeny_penalty;
  6994. }
  6995. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6996. switch (battle_config.death_penalty_type) {
  6997. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6998. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6999. }
  7000. if (base_penalty){ //recheck after altering to speedup
  7001. if (battle_config.pk_mode && src && src->type==BL_PC)
  7002. base_penalty *= 2;
  7003. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7004. }
  7005. }
  7006. else
  7007. base_penalty = 0;
  7008. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7009. switch (battle_config.death_penalty_type) {
  7010. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7011. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7012. }
  7013. if (job_penalty) {
  7014. if (battle_config.pk_mode && src && src->type==BL_PC)
  7015. job_penalty *= 2;
  7016. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7017. }
  7018. }
  7019. else
  7020. job_penalty = 0;
  7021. if (base_penalty || job_penalty)
  7022. pc_lostexp(sd, base_penalty, job_penalty);
  7023. if( zeny_penalty > 0 && !map_getmapflag(sd->bl.m, MF_NOZENYPENALTY)) {
  7024. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7025. if(zeny_penalty)
  7026. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7027. }
  7028. }
  7029. if( map_getmapflag( sd->bl.m, MF_PVP_NIGHTMAREDROP ) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7030. for(int j=0;j<MAX_DROP_PER_MAP;j++){
  7031. int id = map_getmapdata(sd->bl.m)->drop_list[j].drop_id;
  7032. int per = map_getmapdata(sd->bl.m)->drop_list[j].drop_per;
  7033. enum e_nightmare_drop_type type = map_getmapdata(sd->bl.m)->drop_list[j].drop_type;
  7034. if(id == 0)
  7035. continue;
  7036. if(id == -1){
  7037. int eq_num=0,eq_n[MAX_INVENTORY];
  7038. memset(eq_n,0,sizeof(eq_n));
  7039. for(i=0;i<MAX_INVENTORY;i++) {
  7040. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7041. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7042. || type&NMDT_ALL)
  7043. {
  7044. int l;
  7045. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7046. if( l < MAX_INVENTORY )
  7047. eq_n[l] = i;
  7048. eq_num++;
  7049. }
  7050. }
  7051. if(eq_num > 0){
  7052. int n = eq_n[rnd()%eq_num];
  7053. if(rnd()%10000 < per) {
  7054. if(sd->inventory.u.items_inventory[n].equip)
  7055. pc_unequipitem(sd,n,3);
  7056. pc_dropitem(sd,n,1);
  7057. }
  7058. }
  7059. }
  7060. else if(id > 0) {
  7061. for(i=0;i<MAX_INVENTORY;i++){
  7062. if(sd->inventory.u.items_inventory[i].nameid == id
  7063. && rnd()%10000 < per
  7064. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7065. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7066. || type&NMDT_ALL) ){
  7067. if(sd->inventory.u.items_inventory[i].equip)
  7068. pc_unequipitem(sd,i,3);
  7069. pc_dropitem(sd,i,1);
  7070. break;
  7071. }
  7072. }
  7073. }
  7074. }
  7075. }
  7076. // pvp
  7077. // disable certain pvp functions on pk_mode [Valaris]
  7078. if( map_getmapflag(sd->bl.m, MF_PVP) && !battle_config.pk_mode && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) ) {
  7079. sd->pvp_point -= 5;
  7080. sd->pvp_lost++;
  7081. if( src && src->type == BL_PC ) {
  7082. struct map_session_data *ssd = (struct map_session_data *)src;
  7083. ssd->pvp_point++;
  7084. ssd->pvp_won++;
  7085. }
  7086. if( sd->pvp_point < 0 ) {
  7087. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7088. return 1|8;
  7089. }
  7090. }
  7091. //GvG
  7092. if( map_flag_gvg2(sd->bl.m) ) {
  7093. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7094. return 1|8;
  7095. }
  7096. else if( sd->bg_id ) {
  7097. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7098. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7099. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7100. return 1|8;
  7101. }
  7102. }
  7103. //Reset "can log out" tick.
  7104. if( battle_config.prevent_logout )
  7105. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7106. return 1;
  7107. }
  7108. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7109. if(hp) clif_updatestatus(sd,SP_HP);
  7110. if(sp) clif_updatestatus(sd,SP_SP);
  7111. pc_setstand(sd, true);
  7112. if(battle_config.pc_invincible_time > 0)
  7113. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7114. if( sd->state.gmaster_flag ) {
  7115. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7116. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7117. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7118. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7119. }
  7120. }
  7121. // script
  7122. //
  7123. /*==========================================
  7124. * script reading pc status registry
  7125. *------------------------------------------*/
  7126. int pc_readparam(struct map_session_data* sd,int type)
  7127. {
  7128. int val = 0;
  7129. nullpo_ret(sd);
  7130. switch(type) {
  7131. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7132. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7133. case SP_ZENY: val = sd->status.zeny; break;
  7134. case SP_BASELEVEL: val = sd->status.base_level; break;
  7135. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7136. case SP_CLASS: val = sd->status.class_; break;
  7137. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7138. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7139. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7140. case SP_SEX: val = sd->status.sex; break;
  7141. case SP_WEIGHT: val = sd->weight; break;
  7142. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7143. case SP_BASEEXP: val = sd->status.base_exp; break;
  7144. case SP_JOBEXP: val = sd->status.job_exp; break;
  7145. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7146. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7147. case SP_HP: val = sd->battle_status.hp; break;
  7148. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7149. case SP_SP: val = sd->battle_status.sp; break;
  7150. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7151. case SP_STR: val = sd->status.str; break;
  7152. case SP_AGI: val = sd->status.agi; break;
  7153. case SP_VIT: val = sd->status.vit; break;
  7154. case SP_INT: val = sd->status.int_; break;
  7155. case SP_DEX: val = sd->status.dex; break;
  7156. case SP_LUK: val = sd->status.luk; break;
  7157. case SP_KARMA: val = sd->status.karma; break;
  7158. case SP_MANNER: val = sd->status.manner; break;
  7159. case SP_FAME: val = sd->status.fame; break;
  7160. case SP_KILLERRID: val = sd->killerrid; break;
  7161. case SP_KILLEDRID: val = sd->killedrid; break;
  7162. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7163. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7164. case SP_CHARRENAME: val = sd->status.rename; break;
  7165. case SP_CHARFONT: val = sd->status.font; break;
  7166. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7167. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7168. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7169. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7170. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7171. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7172. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7173. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7174. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7175. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7176. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7177. case SP_DEF1: val = sd->battle_status.def; break;
  7178. case SP_DEF2: val = sd->battle_status.def2; break;
  7179. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7180. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7181. case SP_HIT: val = sd->battle_status.hit; break;
  7182. case SP_FLEE1: val = sd->battle_status.flee; break;
  7183. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7184. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7185. case SP_MAXHPRATE: val = sd->hprate; break;
  7186. case SP_MAXSPRATE: val = sd->sprate; break;
  7187. case SP_SPRATE: val = sd->dsprate; break;
  7188. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7189. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7190. case SP_ASPD_RATE:
  7191. #ifndef RENEWAL_ASPD
  7192. val = sd->battle_status.aspd_rate;
  7193. #else
  7194. val = sd->battle_status.aspd_rate2;
  7195. #endif
  7196. break;
  7197. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7198. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7199. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7200. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7201. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7202. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7203. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7204. case SP_MATK_RATE: val = sd->matk_rate; break;
  7205. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7206. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7207. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7208. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7209. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7210. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7211. case SP_HIT_RATE: val = sd->hit_rate; break;
  7212. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7213. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7214. case SP_DEF_RATE: val = sd->def_rate; break;
  7215. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7216. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7217. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7218. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7219. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7220. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7221. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7222. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7223. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7224. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7225. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7226. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7227. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7228. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7229. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7230. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7231. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7232. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7233. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7234. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7235. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7236. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7237. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7238. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7239. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7240. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7241. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7242. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7243. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7244. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7245. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7246. case SP_DELAYRATE: val = sd->delayrate; break;
  7247. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7248. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7249. case SP_UNSTRIPABLE:
  7250. case SP_UNSTRIPABLE_ARMOR:
  7251. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7252. break;
  7253. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7254. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7255. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7256. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7257. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7258. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7259. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7260. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7261. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7262. case SP_EMATK: val = sd->bonus.ematk; break;
  7263. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7264. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7265. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7266. case SP_CASTRATE:
  7267. case SP_VARCASTRATE:
  7268. #ifdef RENEWAL_CAST
  7269. val = sd->bonus.varcastrate; break;
  7270. #else
  7271. val = sd->castrate; break;
  7272. #endif
  7273. default:
  7274. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7275. return -1;
  7276. }
  7277. return val;
  7278. }
  7279. /*==========================================
  7280. * script set pc status registry
  7281. *------------------------------------------*/
  7282. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7283. {
  7284. nullpo_retr(false,sd);
  7285. switch(type){
  7286. case SP_BASELEVEL:
  7287. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7288. val = pc_maxbaselv(sd);
  7289. if ((unsigned int)val > sd->status.base_level) {
  7290. int i = 0;
  7291. int stat=0;
  7292. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7293. stat += pc_gets_status_point(sd->status.base_level + i);
  7294. sd->status.status_point += stat;
  7295. }
  7296. sd->status.base_level = (unsigned int)val;
  7297. sd->status.base_exp = 0;
  7298. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7299. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7300. clif_updatestatus(sd, SP_STATUSPOINT);
  7301. clif_updatestatus(sd, SP_BASEEXP);
  7302. status_calc_pc(sd, SCO_FORCE);
  7303. if(sd->status.party_id)
  7304. party_send_levelup(sd);
  7305. break;
  7306. case SP_JOBLEVEL:
  7307. if ((unsigned int)val >= sd->status.job_level) {
  7308. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7309. sd->status.skill_point += val - sd->status.job_level;
  7310. clif_updatestatus(sd, SP_SKILLPOINT);
  7311. }
  7312. sd->status.job_level = (unsigned int)val;
  7313. sd->status.job_exp = 0;
  7314. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7315. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7316. clif_updatestatus(sd, SP_JOBEXP);
  7317. status_calc_pc(sd, SCO_FORCE);
  7318. break;
  7319. case SP_SKILLPOINT:
  7320. sd->status.skill_point = val;
  7321. break;
  7322. case SP_STATUSPOINT:
  7323. sd->status.status_point = val;
  7324. break;
  7325. case SP_ZENY:
  7326. if( val < 0 )
  7327. return false;// can't set negative zeny
  7328. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7329. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7330. break;
  7331. case SP_BASEEXP:
  7332. {
  7333. val = cap_value(val, 0, INT_MAX);
  7334. if ((unsigned int)val < sd->status.base_exp) // Lost
  7335. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7336. else // Gained
  7337. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7338. }
  7339. return true;
  7340. case SP_JOBEXP:
  7341. {
  7342. val = cap_value(val, 0, INT_MAX);
  7343. if ((unsigned int)val < sd->status.job_exp) // Lost
  7344. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7345. else // Gained
  7346. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7347. }
  7348. return true;
  7349. case SP_SEX:
  7350. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7351. break;
  7352. case SP_WEIGHT:
  7353. sd->weight = val;
  7354. break;
  7355. case SP_MAXWEIGHT:
  7356. sd->max_weight = val;
  7357. break;
  7358. case SP_HP:
  7359. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7360. break;
  7361. case SP_MAXHP:
  7362. if (sd->status.base_level < 100)
  7363. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7364. else if (sd->status.base_level < 151)
  7365. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7366. else
  7367. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7368. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7369. {
  7370. sd->battle_status.hp = sd->battle_status.max_hp;
  7371. clif_updatestatus(sd, SP_HP);
  7372. }
  7373. break;
  7374. case SP_SP:
  7375. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7376. break;
  7377. case SP_MAXSP:
  7378. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7379. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7380. {
  7381. sd->battle_status.sp = sd->battle_status.max_sp;
  7382. clif_updatestatus(sd, SP_SP);
  7383. }
  7384. break;
  7385. case SP_STR:
  7386. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7387. break;
  7388. case SP_AGI:
  7389. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7390. break;
  7391. case SP_VIT:
  7392. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7393. break;
  7394. case SP_INT:
  7395. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7396. break;
  7397. case SP_DEX:
  7398. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7399. break;
  7400. case SP_LUK:
  7401. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7402. break;
  7403. case SP_KARMA:
  7404. sd->status.karma = val;
  7405. break;
  7406. case SP_MANNER:
  7407. sd->status.manner = val;
  7408. if( val < 0 )
  7409. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7410. else {
  7411. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7412. clif_manner_message(sd, 5);
  7413. }
  7414. return true; // status_change_start/status_change_end already sends packets warning the client
  7415. case SP_FAME:
  7416. sd->status.fame = val;
  7417. break;
  7418. case SP_KILLERRID:
  7419. sd->killerrid = val;
  7420. return true;
  7421. case SP_KILLEDRID:
  7422. sd->killedrid = val;
  7423. return true;
  7424. case SP_CHARMOVE:
  7425. sd->status.character_moves = val;
  7426. return true;
  7427. case SP_CHARRENAME:
  7428. sd->status.rename = val;
  7429. return true;
  7430. case SP_CHARFONT:
  7431. sd->status.font = val;
  7432. clif_font(sd);
  7433. return true;
  7434. case SP_BANK_VAULT:
  7435. if (val < 0)
  7436. return false;
  7437. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7438. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7439. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7440. return true;
  7441. case SP_ROULETTE_BRONZE:
  7442. sd->roulette_point.bronze = val;
  7443. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7444. return true;
  7445. case SP_ROULETTE_SILVER:
  7446. sd->roulette_point.silver = val;
  7447. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7448. return true;
  7449. case SP_ROULETTE_GOLD:
  7450. sd->roulette_point.gold = val;
  7451. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7452. return true;
  7453. case SP_CASHPOINTS:
  7454. if (val < 0)
  7455. return false;
  7456. if (!sd->state.connect_new)
  7457. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7458. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7459. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7460. return true;
  7461. case SP_KAFRAPOINTS:
  7462. if (val < 0)
  7463. return false;
  7464. if (!sd->state.connect_new)
  7465. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7466. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7467. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7468. return true;
  7469. case SP_PCDIECOUNTER:
  7470. if (val < 0)
  7471. return false;
  7472. if (sd->die_counter == val)
  7473. return true;
  7474. sd->die_counter = val;
  7475. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7476. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7477. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7478. return true;
  7479. case SP_COOKMASTERY:
  7480. if (val < 0)
  7481. return false;
  7482. if (sd->cook_mastery == val)
  7483. return true;
  7484. val = cap_value(val, 0, 1999);
  7485. sd->cook_mastery = val;
  7486. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7487. return true;
  7488. default:
  7489. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7490. return false;
  7491. }
  7492. clif_updatestatus(sd,type);
  7493. return true;
  7494. }
  7495. /*==========================================
  7496. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7497. *------------------------------------------*/
  7498. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7499. {
  7500. if (type&2) {
  7501. if (hp || type&4)
  7502. clif_heal(sd->fd,SP_HP,hp);
  7503. if (sp)
  7504. clif_heal(sd->fd,SP_SP,sp);
  7505. } else {
  7506. if(hp)
  7507. clif_updatestatus(sd,SP_HP);
  7508. if(sp)
  7509. clif_updatestatus(sd,SP_SP);
  7510. }
  7511. return;
  7512. }
  7513. /**
  7514. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7515. * @param sd: Player data
  7516. * @param itemid: Item ID
  7517. * @param hp: HP to heal
  7518. * @param sp: SP to heal
  7519. * @return Amount healed to an object
  7520. */
  7521. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7522. {
  7523. int bonus, tmp, penalty = 0;
  7524. if (hp) {
  7525. int i;
  7526. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7527. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7528. if (potion_flag == 2) {
  7529. bonus += 50;
  7530. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7531. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7532. }
  7533. //All item bonuses.
  7534. bonus += sd->bonus.itemhealrate2;
  7535. //Item Group bonuses
  7536. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7537. //Individual item bonuses.
  7538. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
  7539. if (sd->itemhealrate[i].nameid == itemid) {
  7540. bonus += sd->itemhealrate[i].rate;
  7541. break;
  7542. }
  7543. }
  7544. // Recovery Potion
  7545. if (sd->sc.data[SC_INCHEALRATE])
  7546. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7547. // 2014 Halloween Event : Pumpkin Bonus
  7548. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7549. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7550. tmp = hp * bonus / 100; // Overflow check
  7551. if (bonus != 100 && tmp > hp)
  7552. hp = tmp;
  7553. }
  7554. if (sp) {
  7555. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7556. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7557. if (potion_flag == 2)
  7558. bonus += 50;
  7559. tmp = sp * bonus / 100; // Overflow check
  7560. if (bonus != 100 && tmp > sp)
  7561. sp = tmp;
  7562. }
  7563. if (sd->sc.count) {
  7564. // Critical Wound and Death Hurt stack
  7565. if (sd->sc.data[SC_CRITICALWOUND])
  7566. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7567. if (sd->sc.data[SC_DEATHHURT])
  7568. penalty += 20;
  7569. if (sd->sc.data[SC_NORECOVER_STATE])
  7570. penalty = 100;
  7571. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7572. hp += hp / 2; // 1.5 times
  7573. sp -= sp / 2;
  7574. }
  7575. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7576. hp += hp / 10;
  7577. sp += sp / 10;
  7578. }
  7579. if (penalty > 0) {
  7580. hp -= hp * penalty / 100;
  7581. sp -= sp * penalty / 100;
  7582. }
  7583. #ifdef RENEWAL
  7584. if (sd->sc.data[SC_EXTREMITYFIST2])
  7585. sp = 0;
  7586. #endif
  7587. if (sd->sc.data[SC_BITESCAR])
  7588. hp = 0;
  7589. }
  7590. return status_heal(&sd->bl, hp, sp, 1);
  7591. }
  7592. /*==========================================
  7593. * HP/SP Recovery
  7594. * Heal player hp nad/or sp by rate
  7595. *------------------------------------------*/
  7596. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7597. {
  7598. nullpo_ret(sd);
  7599. if (hp > 100) hp = 100;
  7600. else if (hp <-100) hp = -100;
  7601. if (sp > 100) sp = 100;
  7602. else if (sp <-100) sp = -100;
  7603. if(hp >= 0 && sp >= 0) //Heal
  7604. return status_percent_heal(&sd->bl, hp, sp);
  7605. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7606. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7607. //Crossed signs
  7608. if(hp) {
  7609. if(hp > 0)
  7610. status_percent_heal(&sd->bl, hp, 0);
  7611. else
  7612. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7613. }
  7614. if(sp) {
  7615. if(sp > 0)
  7616. status_percent_heal(&sd->bl, 0, sp);
  7617. else
  7618. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7619. }
  7620. return 0;
  7621. }
  7622. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7623. {
  7624. struct mob_data *md;
  7625. int flag;
  7626. md = (struct mob_data *)bl;
  7627. nullpo_ret(md);
  7628. flag = va_arg(ap, int);
  7629. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7630. status_kill(&md->bl);
  7631. return 1;
  7632. }
  7633. /**
  7634. * Called when player changes job
  7635. * Rewrote to make it tidider [Celest]
  7636. * @param sd
  7637. * @param job JOB ID. See enum e_job
  7638. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7639. * @return True if success, false if failed
  7640. **/
  7641. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7642. {
  7643. int i, fame_flag = 0;
  7644. int b_class;
  7645. nullpo_retr(false,sd);
  7646. if (job < 0)
  7647. return false;
  7648. //Normalize job.
  7649. b_class = pc_jobid2mapid(job);
  7650. if (b_class == -1)
  7651. return false;
  7652. switch (upper) {
  7653. case 1:
  7654. b_class|= JOBL_UPPER;
  7655. break;
  7656. case 2:
  7657. b_class|= JOBL_BABY;
  7658. break;
  7659. }
  7660. //This will automatically adjust bard/dancer classes to the correct gender
  7661. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7662. job = pc_mapid2jobid(b_class, sd->status.sex);
  7663. if (job == -1)
  7664. return false;
  7665. if ((unsigned short)b_class == sd->class_)
  7666. return false; //Nothing to change.
  7667. // changing from 1st to 2nd job
  7668. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7669. sd->change_level_2nd = sd->status.job_level;
  7670. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7671. }
  7672. // changing from 2nd to 3rd job
  7673. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7674. sd->change_level_3rd = sd->status.job_level;
  7675. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7676. }
  7677. if(sd->cloneskill_idx > 0) {
  7678. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7679. sd->status.skill[sd->cloneskill_idx].id = 0;
  7680. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7681. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7682. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7683. }
  7684. sd->cloneskill_idx = 0;
  7685. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7686. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7687. }
  7688. if(sd->reproduceskill_idx > 0) {
  7689. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7690. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7691. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7692. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7693. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7694. }
  7695. sd->reproduceskill_idx = 0;
  7696. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7697. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7698. }
  7699. // Give or reduce transcendent status points
  7700. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7701. sd->status.status_point += battle_config.transcendent_status_points;
  7702. clif_updatestatus(sd,SP_STATUSPOINT);
  7703. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7704. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7705. // The player already used his bonus points, so we have to reset his status points
  7706. pc_resetstate(sd);
  7707. }
  7708. sd->status.status_point -= battle_config.transcendent_status_points;
  7709. clif_updatestatus(sd,SP_STATUSPOINT);
  7710. }
  7711. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7712. const int class_ = pc_class2idx(sd->status.class_);
  7713. uint16 skill_id;
  7714. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7715. //Remove status specific to your current tree skills.
  7716. enum sc_type sc = status_skill2sc(skill_id);
  7717. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7718. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7719. }
  7720. }
  7721. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7722. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7723. pc_resetfeel(sd);
  7724. }
  7725. // Reset body style to 0 before changing job to avoid
  7726. // errors since not every job has a alternate outfit.
  7727. sd->status.body = 0;
  7728. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7729. sd->status.class_ = job;
  7730. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7731. sd->class_ = (unsigned short)b_class;
  7732. sd->status.job_level=1;
  7733. sd->status.job_exp=0;
  7734. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7735. sd->status.base_level = pc_maxbaselv(sd);
  7736. sd->status.base_exp=0;
  7737. pc_resetstate(sd);
  7738. clif_updatestatus(sd,SP_STATUSPOINT);
  7739. clif_updatestatus(sd,SP_BASELEVEL);
  7740. clif_updatestatus(sd,SP_BASEEXP);
  7741. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7742. }
  7743. clif_updatestatus(sd,SP_JOBLEVEL);
  7744. clif_updatestatus(sd,SP_JOBEXP);
  7745. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7746. for(i=0;i<EQI_MAX;i++) {
  7747. if(sd->equip_index[i] >= 0)
  7748. if(pc_isequip(sd,sd->equip_index[i]))
  7749. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7750. }
  7751. //Change look, if disguised, you need to undisguise
  7752. //to correctly calculate new job sprite without
  7753. if (sd->disguise)
  7754. pc_disguise(sd, 0);
  7755. status_set_viewdata(&sd->bl, job);
  7756. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7757. #if PACKETVER >= 20151001
  7758. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7759. #endif
  7760. if(sd->vd.cloth_color)
  7761. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7762. /*
  7763. if(sd->vd.body_style)
  7764. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7765. */
  7766. //Update skill tree.
  7767. pc_calc_skilltree(sd);
  7768. clif_skillinfoblock(sd);
  7769. if (sd->ed)
  7770. elemental_delete(sd->ed);
  7771. if (sd->state.vending)
  7772. vending_closevending(sd);
  7773. if (sd->state.buyingstore)
  7774. buyingstore_close(sd);
  7775. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7776. //Remove peco/cart/falcon
  7777. i = sd->sc.option;
  7778. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7779. i&=~OPTION_RIDING;
  7780. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7781. i&=~OPTION_FALCON;
  7782. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7783. i&=~OPTION_DRAGON;
  7784. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7785. i&=~OPTION_WUGRIDER;
  7786. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7787. i&=~OPTION_WUG;
  7788. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7789. i&=~OPTION_MADOGEAR;
  7790. #ifndef NEW_CARTS
  7791. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7792. i&=~OPTION_CART;
  7793. #else
  7794. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7795. pc_setcart(sd, 0);
  7796. #endif
  7797. if(i != sd->sc.option)
  7798. pc_setoption(sd, i);
  7799. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7800. hom_vaporize(sd, HOM_ST_ACTIVE);
  7801. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7802. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7803. if(sd->status.manner < 0)
  7804. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7805. status_calc_pc(sd,SCO_FORCE);
  7806. pc_checkallowskill(sd);
  7807. pc_equiplookall(sd);
  7808. pc_show_questinfo(sd);
  7809. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7810. if( sd->status.party_id ){
  7811. struct party_data* p;
  7812. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7813. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7814. if( i < MAX_PARTY ){
  7815. p->party.member[i].class_ = sd->status.class_;
  7816. clif_party_job_and_level(sd);
  7817. }
  7818. }
  7819. }
  7820. chrif_save(sd, CSAVE_NORMAL);
  7821. //if you were previously famous, not anymore.
  7822. if (fame_flag)
  7823. chrif_buildfamelist();
  7824. else if (sd->status.fame > 0) {
  7825. //It may be that now they are famous?
  7826. switch (sd->class_&MAPID_UPPERMASK) {
  7827. case MAPID_BLACKSMITH:
  7828. case MAPID_ALCHEMIST:
  7829. case MAPID_TAEKWON:
  7830. chrif_buildfamelist();
  7831. break;
  7832. }
  7833. }
  7834. return true;
  7835. }
  7836. /*==========================================
  7837. * Tell client player sd has change equipement
  7838. *------------------------------------------*/
  7839. void pc_equiplookall(struct map_session_data *sd)
  7840. {
  7841. nullpo_retv(sd);
  7842. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7843. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7844. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7845. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7846. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7847. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7848. }
  7849. /*==========================================
  7850. * Tell client player sd has change look (hair,equip...)
  7851. *------------------------------------------*/
  7852. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7853. nullpo_retv(sd);
  7854. switch(type) {
  7855. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7856. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7857. if (sd->status.hair != val) {
  7858. sd->status.hair = val;
  7859. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7860. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7861. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7862. }
  7863. break;
  7864. case LOOK_WEAPON:
  7865. sd->status.weapon = val;
  7866. break;
  7867. case LOOK_HEAD_BOTTOM:
  7868. sd->status.head_bottom = val;
  7869. sd->setlook_head_bottom = val;
  7870. break;
  7871. case LOOK_HEAD_TOP:
  7872. sd->status.head_top = val;
  7873. sd->setlook_head_top = val;
  7874. break;
  7875. case LOOK_HEAD_MID:
  7876. sd->status.head_mid = val;
  7877. sd->setlook_head_mid = val;
  7878. break;
  7879. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7880. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7881. if (sd->status.hair_color != val) {
  7882. sd->status.hair_color = val;
  7883. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7884. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7885. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7886. }
  7887. break;
  7888. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7889. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7890. sd->status.clothes_color = val;
  7891. break;
  7892. case LOOK_SHIELD:
  7893. sd->status.shield = val;
  7894. break;
  7895. case LOOK_SHOES:
  7896. break;
  7897. case LOOK_ROBE:
  7898. sd->status.robe = val;
  7899. sd->setlook_robe = val;
  7900. break;
  7901. case LOOK_BODY2:
  7902. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7903. sd->status.body = val;
  7904. break;
  7905. }
  7906. clif_changelook(&sd->bl, type, val);
  7907. }
  7908. /*==========================================
  7909. * Give an option (type) to player (sd) and display it to client
  7910. *------------------------------------------*/
  7911. void pc_setoption(struct map_session_data *sd,int type)
  7912. {
  7913. int p_type, new_look=0;
  7914. nullpo_retv(sd);
  7915. p_type = sd->sc.option;
  7916. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7917. sd->sc.option=type;
  7918. clif_changeoption(&sd->bl);
  7919. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7920. { // Mounting
  7921. clif_status_load(&sd->bl,EFST_RIDING,1);
  7922. status_calc_pc(sd,SCO_NONE);
  7923. }
  7924. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7925. { // Dismount
  7926. clif_status_load(&sd->bl,EFST_RIDING,0);
  7927. status_calc_pc(sd,SCO_NONE);
  7928. }
  7929. #ifndef NEW_CARTS
  7930. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7931. clif_cartlist(sd);
  7932. clif_updatestatus(sd, SP_CARTINFO);
  7933. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7934. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7935. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7936. clif_clearcart(sd->fd);
  7937. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7938. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7939. }
  7940. #endif
  7941. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7942. clif_status_load(&sd->bl,EFST_FALCON,1);
  7943. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7944. clif_status_load(&sd->bl,EFST_FALCON,0);
  7945. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7946. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7947. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  7948. status_calc_pc(sd,SCO_NONE);
  7949. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7950. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  7951. status_calc_pc(sd,SCO_NONE);
  7952. }
  7953. }
  7954. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7955. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7956. status_calc_pc(sd,SCO_NONE);
  7957. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7958. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7959. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7960. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7961. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7962. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7963. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7964. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7965. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7966. status_calc_pc(sd,SCO_NONE);
  7967. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7968. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7969. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7970. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7971. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7972. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  7973. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  7974. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  7975. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7976. }
  7977. }
  7978. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7979. new_look = JOB_STAR_GLADIATOR2;
  7980. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7981. new_look = -1;
  7982. if (sd->disguise || !new_look)
  7983. return; //Disguises break sprite changes
  7984. if (new_look < 0) { //Restore normal look.
  7985. status_set_viewdata(&sd->bl, sd->status.class_);
  7986. new_look = sd->vd.class_;
  7987. }
  7988. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7989. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7990. if (sd->vd.cloth_color)
  7991. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7992. if( sd->vd.body_style )
  7993. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7994. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7995. }
  7996. /**
  7997. * Give player a cart
  7998. * @param sd Player
  7999. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8000. **/
  8001. bool pc_setcart(struct map_session_data *sd,int type) {
  8002. #ifndef NEW_CARTS
  8003. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8004. int option;
  8005. #endif
  8006. nullpo_retr(false,sd);
  8007. if( type < 0 || type > MAX_CARTS )
  8008. return false;// Never trust the values sent by the client! [Skotlex]
  8009. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8010. return false;// Push cart is required
  8011. #ifdef NEW_CARTS
  8012. switch( type ) {
  8013. case 0:
  8014. if( !sd->sc.data[SC_PUSH_CART] )
  8015. return 0;
  8016. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8017. clif_clearcart(sd->fd);
  8018. break;
  8019. default:/* everything else is an allowed ID so we can move on */
  8020. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8021. clif_cartlist(sd);
  8022. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8023. }
  8024. clif_updatestatus(sd, SP_CARTINFO);
  8025. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8026. break;
  8027. }
  8028. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8029. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8030. #else
  8031. // Update option
  8032. option = sd->sc.option;
  8033. option &= ~OPTION_CART;// clear cart bits
  8034. option |= cart[type]; // set cart
  8035. pc_setoption(sd, option);
  8036. #endif
  8037. return true;
  8038. }
  8039. /*==========================================
  8040. * Give player a falcon
  8041. *------------------------------------------*/
  8042. void pc_setfalcon(struct map_session_data* sd, int flag)
  8043. {
  8044. if( flag ){
  8045. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8046. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8047. } else if( pc_isfalcon(sd) ){
  8048. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8049. }
  8050. }
  8051. /*==========================================
  8052. * Set player riding
  8053. *------------------------------------------*/
  8054. void pc_setriding(struct map_session_data* sd, int flag)
  8055. {
  8056. if( sd->sc.data[SC_ALL_RIDING] )
  8057. return;
  8058. if( flag ){
  8059. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8060. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8061. } else if( pc_isriding(sd) ){
  8062. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8063. }
  8064. }
  8065. /*==========================================
  8066. * Give player a mado
  8067. *------------------------------------------*/
  8068. void pc_setmadogear(struct map_session_data* sd, int flag)
  8069. {
  8070. if( flag ){
  8071. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8072. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8073. } else if( pc_ismadogear(sd) ){
  8074. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8075. }
  8076. }
  8077. /*==========================================
  8078. * Check if player can drop an item
  8079. *------------------------------------------*/
  8080. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8081. {
  8082. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  8083. return false;
  8084. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8085. return false;
  8086. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8087. }
  8088. /**
  8089. * Determines whether a player can attack based on status changes
  8090. * Why not use status_check_skilluse?
  8091. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8092. * Even ground-based attacks should be blocked by these statuses
  8093. * Called from unit_attack and unit_attack_timer_sub
  8094. * @retval true Can attack
  8095. **/
  8096. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8097. nullpo_retr(false, sd);
  8098. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8099. return false;
  8100. if( sd->sc.data[SC_BASILICA] ||
  8101. sd->sc.data[SC__SHADOWFORM] ||
  8102. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8103. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8104. sd->sc.data[SC_CRYSTALIZE] ||
  8105. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8106. sd->sc.data[SC_TRICKDEAD] ||
  8107. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8108. sd->sc.data[SC_BLADESTOP] ||
  8109. sd->sc.data[SC_DEEPSLEEP] ||
  8110. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8111. sd->sc.data[SC_KINGS_GRACE] )
  8112. return false;
  8113. return true;
  8114. }
  8115. /*==========================================
  8116. * Read '@type' variables (temporary numeric char reg)
  8117. *------------------------------------------*/
  8118. int pc_readreg(struct map_session_data* sd, int64 reg)
  8119. {
  8120. return i64db_iget(sd->regs.vars, reg);
  8121. }
  8122. /*==========================================
  8123. * Set '@type' variables (temporary numeric char reg)
  8124. *------------------------------------------*/
  8125. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8126. {
  8127. unsigned int index = script_getvaridx(reg);
  8128. nullpo_retr(false, sd);
  8129. if( val ) {
  8130. i64db_iput(sd->regs.vars, reg, val);
  8131. if( index )
  8132. script_array_update(&sd->regs, reg, false);
  8133. } else {
  8134. i64db_remove(sd->regs.vars, reg);
  8135. if( index )
  8136. script_array_update(&sd->regs, reg, true);
  8137. }
  8138. return true;
  8139. }
  8140. /*==========================================
  8141. * Read '@type$' variables (temporary string char reg)
  8142. *------------------------------------------*/
  8143. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8144. {
  8145. struct script_reg_str *p = NULL;
  8146. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8147. return p ? p->value : NULL;
  8148. }
  8149. /*==========================================
  8150. * Set '@type$' variables (temporary string char reg)
  8151. *------------------------------------------*/
  8152. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8153. {
  8154. struct script_reg_str *p = NULL;
  8155. unsigned int index = script_getvaridx(reg);
  8156. DBData prev;
  8157. nullpo_retr(false, sd);
  8158. if( str[0] ) {
  8159. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8160. p->value = aStrdup(str);
  8161. p->flag.type = 1;
  8162. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8163. p = (struct script_reg_str *)db_data2ptr(&prev);
  8164. if( p->value )
  8165. aFree(p->value);
  8166. ers_free(str_reg_ers, p);
  8167. } else {
  8168. if( index )
  8169. script_array_update(&sd->regs, reg, false);
  8170. }
  8171. } else {
  8172. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8173. p = (struct script_reg_str *)db_data2ptr(&prev);
  8174. if( p->value )
  8175. aFree(p->value);
  8176. ers_free(str_reg_ers, p);
  8177. if( index )
  8178. script_array_update(&sd->regs, reg, true);
  8179. }
  8180. }
  8181. return true;
  8182. }
  8183. /**
  8184. * Serves the following variable types:
  8185. * - 'type' (permanent numeric char reg)
  8186. * - '#type' (permanent numeric account reg)
  8187. * - '##type' (permanent numeric account reg2)
  8188. **/
  8189. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8190. {
  8191. struct script_reg_num *p = NULL;
  8192. if (!sd->vars_ok) {
  8193. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8194. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8195. //intif->request_registry(sd,type==3?4:type);
  8196. set_eof(sd->fd);
  8197. return 0;
  8198. }
  8199. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8200. return p ? p->value : 0;
  8201. }
  8202. /**
  8203. * Serves the following variable types:
  8204. * - 'type$' (permanent str char reg)
  8205. * - '#type$' (permanent str account reg)
  8206. * - '##type$' (permanent str account reg2)
  8207. **/
  8208. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8209. {
  8210. struct script_reg_str *p = NULL;
  8211. if (!sd->vars_ok) {
  8212. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8213. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8214. //intif->request_registry(sd,type==3?4:type);
  8215. set_eof(sd->fd);
  8216. return NULL;
  8217. }
  8218. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8219. return p ? p->value : NULL;
  8220. }
  8221. /**
  8222. * Serves the following variable types:
  8223. * - 'type' (permanent numeric char reg)
  8224. * - '#type' (permanent numeric account reg)
  8225. * - '##type' (permanent numeric account reg2)
  8226. **/
  8227. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8228. {
  8229. struct script_reg_num *p = NULL;
  8230. const char *regname = get_str(script_getvarid(reg));
  8231. unsigned int index = script_getvaridx(reg);
  8232. if ( !reg_load && !sd->vars_ok ) {
  8233. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8234. return 0;
  8235. }
  8236. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8237. if( val ) {
  8238. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8239. script_array_update(&sd->regs, reg, false);
  8240. p->value = val;
  8241. } else {
  8242. p->value = 0;
  8243. if( index )
  8244. script_array_update(&sd->regs, reg, true);
  8245. }
  8246. if (!reg_load)
  8247. p->flag.update = 1;/* either way, it will require either delete or replace */
  8248. } else if( val ) {
  8249. DBData prev;
  8250. if( index )
  8251. script_array_update(&sd->regs, reg, false);
  8252. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8253. p->value = val;
  8254. if (!reg_load)
  8255. p->flag.update = 1;
  8256. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8257. p = (struct script_reg_num *)db_data2ptr(&prev);
  8258. ers_free(num_reg_ers, p);
  8259. }
  8260. }
  8261. if (!reg_load && p)
  8262. sd->vars_dirty = true;
  8263. return 1;
  8264. }
  8265. /**
  8266. * Serves the following variable types:
  8267. * - 'type$' (permanent str char reg)
  8268. * - '#type$' (permanent str account reg)
  8269. * - '##type$' (permanent str account reg2)
  8270. **/
  8271. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8272. {
  8273. struct script_reg_str *p = NULL;
  8274. const char *regname = get_str(script_getvarid(reg));
  8275. unsigned int index = script_getvaridx(reg);
  8276. size_t vlen = 0;
  8277. if (!reg_load && !sd->vars_ok) {
  8278. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8279. return 0;
  8280. }
  8281. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8282. {
  8283. ShowError("pc_check_RegistryVariableLength: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%zu\n", sd->status.account_id, sd->status.char_id, val, vlen);
  8284. return 0;
  8285. }
  8286. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8287. if( val[0] ) {
  8288. if( p->value )
  8289. aFree(p->value);
  8290. else if ( index ) // an entry that was deleted, so we reset
  8291. script_array_update(&sd->regs, reg, false);
  8292. p->value = aStrdup(val);
  8293. } else {
  8294. if (p->value)
  8295. aFree(p->value);
  8296. p->value = NULL;
  8297. if( index )
  8298. script_array_update(&sd->regs, reg, true);
  8299. }
  8300. if( !reg_load )
  8301. p->flag.update = 1; // either way, it will require either delete or replace
  8302. } else if( val[0] ) {
  8303. DBData prev;
  8304. if( index )
  8305. script_array_update(&sd->regs, reg, false);
  8306. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8307. p->value = aStrdup(val);
  8308. if( !reg_load )
  8309. p->flag.update = 1;
  8310. p->flag.type = 1;
  8311. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8312. p = (struct script_reg_str *)db_data2ptr(&prev);
  8313. if( p->value )
  8314. aFree(p->value);
  8315. ers_free(str_reg_ers, p);
  8316. }
  8317. }
  8318. if( !reg_load && p )
  8319. sd->vars_dirty = true;
  8320. return 1;
  8321. }
  8322. /**
  8323. * Set value of player variable
  8324. * @param sd Player
  8325. * @param reg Variable name
  8326. * @param value
  8327. * @return True if success, false if failed.
  8328. **/
  8329. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8330. char prefix = reg[0];
  8331. nullpo_retr(false, sd);
  8332. if (reg[strlen(reg)-1] == '$') {
  8333. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8334. return false;
  8335. }
  8336. val = cap_value(val, INT_MIN, INT_MAX);
  8337. switch (prefix) {
  8338. case '.':
  8339. case '\'':
  8340. case '$':
  8341. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8342. return false;
  8343. case '@':
  8344. return pc_setreg(sd, add_str(reg), val);
  8345. case '#':
  8346. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8347. default:
  8348. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8349. }
  8350. return false;
  8351. }
  8352. /**
  8353. * Get value of player variable
  8354. * @param sd Player
  8355. * @param reg Variable name
  8356. * @return Variable value or 0 if failed.
  8357. **/
  8358. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8359. char prefix = reg[0];
  8360. nullpo_ret(sd);
  8361. if (reg[strlen(reg)-1] == '$') {
  8362. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8363. return 0;
  8364. }
  8365. switch (prefix) {
  8366. case '.':
  8367. case '\'':
  8368. case '$':
  8369. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8370. return 0;
  8371. case '@':
  8372. return pc_readreg(sd, add_str(reg));
  8373. case '#':
  8374. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8375. default:
  8376. return pc_readglobalreg(sd, add_str(reg));
  8377. }
  8378. return 0;
  8379. }
  8380. /*==========================================
  8381. * Exec eventtimer for player sd (retrieved from map_session (id))
  8382. *------------------------------------------*/
  8383. static TIMER_FUNC(pc_eventtimer){
  8384. struct map_session_data *sd=map_id2sd(id);
  8385. char *p = (char *)data;
  8386. int i;
  8387. if(sd==NULL)
  8388. return 0;
  8389. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8390. if( i < MAX_EVENTTIMER )
  8391. {
  8392. sd->eventtimer[i] = INVALID_TIMER;
  8393. sd->eventcount--;
  8394. npc_event(sd,p,0);
  8395. }
  8396. else
  8397. ShowError("pc_eventtimer: no such event timer\n");
  8398. if (p) aFree(p);
  8399. return 0;
  8400. }
  8401. /*==========================================
  8402. * Add eventtimer for player sd ?
  8403. *------------------------------------------*/
  8404. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8405. {
  8406. int i;
  8407. nullpo_retr(false,sd);
  8408. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8409. if( i == MAX_EVENTTIMER )
  8410. return false;
  8411. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8412. sd->eventcount++;
  8413. return true;
  8414. }
  8415. /*==========================================
  8416. * Del eventtimer for player sd ?
  8417. *------------------------------------------*/
  8418. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8419. {
  8420. char* p = NULL;
  8421. int i;
  8422. nullpo_retr(false,sd);
  8423. if (sd->eventcount == 0)
  8424. return false;
  8425. // find the named event timer
  8426. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8427. sd->eventtimer[i] != INVALID_TIMER &&
  8428. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8429. strcmp(p, name) == 0
  8430. );
  8431. if( i == MAX_EVENTTIMER )
  8432. return false; // not found
  8433. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8434. sd->eventtimer[i] = INVALID_TIMER;
  8435. if(sd->eventcount > 0)
  8436. sd->eventcount--;
  8437. aFree(p);
  8438. return true;
  8439. }
  8440. /*==========================================
  8441. * Update eventtimer count for player sd
  8442. *------------------------------------------*/
  8443. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8444. {
  8445. int i;
  8446. nullpo_retv(sd);
  8447. for(i=0;i<MAX_EVENTTIMER;i++)
  8448. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8449. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8450. addtick_timer(sd->eventtimer[i],tick);
  8451. break;
  8452. }
  8453. }
  8454. /*==========================================
  8455. * Remove all eventtimer for player sd
  8456. *------------------------------------------*/
  8457. void pc_cleareventtimer(struct map_session_data *sd)
  8458. {
  8459. int i;
  8460. nullpo_retv(sd);
  8461. if (sd->eventcount == 0)
  8462. return;
  8463. for(i=0;i<MAX_EVENTTIMER;i++){
  8464. if( sd->eventtimer[i] != INVALID_TIMER ){
  8465. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8466. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8467. sd->eventtimer[i] = INVALID_TIMER;
  8468. if(sd->eventcount > 0) //avoid looping to max val
  8469. sd->eventcount--;
  8470. if (p) aFree(p);
  8471. }
  8472. }
  8473. }
  8474. /**
  8475. * Called when an item with combo is worn
  8476. * @param *sd
  8477. * @param *data struct item_data
  8478. * @return success numbers of succeed combo
  8479. */
  8480. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8481. uint16 i;
  8482. int success = 0;
  8483. for( i = 0; i < data->combos_count; i++ ) {
  8484. struct itemchk {
  8485. int idx;
  8486. unsigned short nameid;
  8487. short card[MAX_SLOTS];
  8488. } *combo_idx;
  8489. int idx, j;
  8490. int nb_itemCombo;
  8491. unsigned int pos = 0;
  8492. /* ensure this isn't a duplicate combo */
  8493. if( sd->combos.bonus != NULL ) {
  8494. int x;
  8495. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8496. /* found a match, skip this combo */
  8497. if( x < sd->combos.count )
  8498. continue;
  8499. }
  8500. nb_itemCombo = data->combos[i]->count;
  8501. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8502. continue;
  8503. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8504. for(j=0; j < nb_itemCombo; j++){
  8505. combo_idx[j].idx=-1;
  8506. combo_idx[j].nameid=-1;
  8507. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8508. }
  8509. for( j = 0; j < nb_itemCombo; j++ ) {
  8510. uint16 id = data->combos[i]->nameid[j], k;
  8511. bool found = false;
  8512. for( k = 0; k < EQI_MAX; k++ ) {
  8513. short index = sd->equip_index[k];
  8514. if( index < 0 )
  8515. continue;
  8516. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8517. continue;
  8518. if (!sd->inventory_data[index] )
  8519. continue;
  8520. if ( itemdb_type(id) != IT_CARD ) {
  8521. if ( sd->inventory_data[index]->nameid != id )
  8522. continue;
  8523. if(j>0){ //check if this item not already used
  8524. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8525. uint8 z;
  8526. for (z = 0; z < nb_itemCombo-1; z++)
  8527. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8528. do_continue=true;
  8529. if(do_continue)
  8530. continue;
  8531. }
  8532. combo_idx[j].nameid = id;
  8533. combo_idx[j].idx = index;
  8534. pos |= sd->inventory.u.items_inventory[index].equip;
  8535. found = true;
  8536. break;
  8537. } else { //Cards and enchants
  8538. uint16 z;
  8539. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8540. continue;
  8541. for (z = 0; z < MAX_SLOTS; z++) {
  8542. bool do_continue=false;
  8543. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8544. continue;
  8545. if(j>0){
  8546. int c1, c2;
  8547. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8548. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8549. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8550. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8551. do_continue = true;
  8552. break;
  8553. }
  8554. }
  8555. }
  8556. }
  8557. }
  8558. if(do_continue)
  8559. continue;
  8560. combo_idx[j].nameid = id;
  8561. combo_idx[j].idx = index;
  8562. combo_idx[j].card[z] = id;
  8563. pos |= sd->inventory.u.items_inventory[index].equip;
  8564. found = true;
  8565. break;
  8566. }
  8567. }
  8568. }
  8569. if( !found )
  8570. break;/* we haven't found all the ids for this combo, so we can return */
  8571. }
  8572. aFree(combo_idx);
  8573. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8574. if( j < nb_itemCombo )
  8575. continue;
  8576. /* we got here, means all items in the combo are matching */
  8577. idx = sd->combos.count;
  8578. if( sd->combos.bonus == NULL ) {
  8579. CREATE(sd->combos.bonus, struct script_code *, 1);
  8580. CREATE(sd->combos.id, unsigned short, 1);
  8581. CREATE(sd->combos.pos, unsigned int, 1);
  8582. sd->combos.count = 1;
  8583. } else {
  8584. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8585. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8586. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8587. }
  8588. /* we simply copy the pointer */
  8589. sd->combos.bonus[idx] = data->combos[i]->script;
  8590. /* save this combo's id */
  8591. sd->combos.id[idx] = data->combos[i]->id;
  8592. /* save pos of combo*/
  8593. sd->combos.pos[idx] = pos;
  8594. success++;
  8595. }
  8596. return success;
  8597. }
  8598. /**
  8599. * Called when an item with combo is removed
  8600. * @param *sd
  8601. * @param *data struct item_data
  8602. * @return retval numbers of removed combo
  8603. */
  8604. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8605. int i, retval = 0;
  8606. if( sd->combos.bonus == NULL )
  8607. return 0;/* nothing to do here, player has no combos */
  8608. for( i = 0; i < data->combos_count; i++ ) {
  8609. /* check if this combo exists in this user */
  8610. int x = 0, cursor = 0, j;
  8611. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8612. /* no match, skip this combo */
  8613. if(x >= sd->combos.count)
  8614. continue;
  8615. sd->combos.bonus[x] = NULL;
  8616. sd->combos.id[x] = 0;
  8617. sd->combos.pos[x] = 0;
  8618. retval++;
  8619. /* check if combo requirements still fit */
  8620. if( pc_checkcombo( sd, data ) )
  8621. continue;
  8622. /* move next value to empty slot */
  8623. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8624. if( sd->combos.bonus[j] == NULL )
  8625. continue;
  8626. if( cursor != j ) {
  8627. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8628. sd->combos.id[cursor] = sd->combos.id[j];
  8629. sd->combos.pos[cursor] = sd->combos.pos[j];
  8630. }
  8631. cursor++;
  8632. }
  8633. /* it's empty, we can clear all the memory */
  8634. if( (sd->combos.count = cursor) == 0 ) {
  8635. aFree(sd->combos.bonus);
  8636. aFree(sd->combos.id);
  8637. aFree(sd->combos.pos);
  8638. sd->combos.bonus = NULL;
  8639. sd->combos.id = NULL;
  8640. sd->combos.pos = NULL;
  8641. return retval; /* we also can return at this point for we have no more combos to check */
  8642. }
  8643. }
  8644. return retval;
  8645. }
  8646. /**
  8647. * Load combo data(s) of player
  8648. * @param *sd
  8649. * @return ret numbers of succeed combo
  8650. */
  8651. int pc_load_combo(struct map_session_data *sd) {
  8652. int i, ret = 0;
  8653. for( i = 0; i < EQI_MAX; i++ ) {
  8654. struct item_data *id = NULL;
  8655. short idx = sd->equip_index[i];
  8656. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8657. continue;
  8658. if( id->combos_count )
  8659. ret += pc_checkcombo(sd,id);
  8660. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8661. struct item_data *data;
  8662. int j;
  8663. for( j = 0; j < id->slot; j++ ) {
  8664. if (!sd->inventory.u.items_inventory[idx].card[j])
  8665. continue;
  8666. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8667. if( data->combos_count )
  8668. ret += pc_checkcombo(sd,data);
  8669. }
  8670. }
  8671. }
  8672. }
  8673. return ret;
  8674. }
  8675. /*==========================================
  8676. * Equip item on player sd at req_pos from inventory index n
  8677. * return: false - fail; true - success
  8678. *------------------------------------------*/
  8679. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8680. {
  8681. int i, pos, flag = 0, iflag;
  8682. struct item_data *id;
  8683. uint8 res = ITEM_EQUIP_ACK_OK;
  8684. nullpo_retr(false,sd);
  8685. if( n < 0 || n >= MAX_INVENTORY ) {
  8686. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8687. return false;
  8688. }
  8689. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8690. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8691. return false;
  8692. }
  8693. if (!(id = sd->inventory_data[n]))
  8694. return false;
  8695. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8696. if(battle_config.battle_log)
  8697. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8698. if((res = pc_isequip(sd,n))) {
  8699. clif_equipitemack(sd,n,0,res); // fail
  8700. return false;
  8701. }
  8702. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8703. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8704. return false;
  8705. }
  8706. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8707. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8708. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8709. return false;
  8710. }
  8711. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8712. /** Failing condition:
  8713. * 1. Always failed to equip ammo if no weapon equipped yet
  8714. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8715. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8716. * (4. The rest is relying on item job/class restriction).
  8717. **/
  8718. if (id->type == IT_AMMO) {
  8719. int w_idx = sd->equip_index[EQI_HAND_R];
  8720. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8721. if (w_idx == -1 ||
  8722. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8723. (id->look != A_GRENADE && w_type == W_GRENADE))
  8724. {
  8725. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8726. return false;
  8727. }
  8728. }
  8729. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8730. int a_idx = sd->equip_index[EQI_AMMO];
  8731. if (a_idx != -1) {
  8732. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8733. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8734. (a_type != A_GRENADE && id->look == W_GRENADE))
  8735. {
  8736. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8737. return false;
  8738. }
  8739. }
  8740. }
  8741. }
  8742. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8743. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8744. clif_notify_bindOnEquip(sd,n);
  8745. }
  8746. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8747. pos = req_pos&EQP_ACC;
  8748. if (pos == EQP_ACC) //User specified both slots..
  8749. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8750. }
  8751. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8752. pos = (req_pos&EQP_ARMS);
  8753. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8754. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8755. }
  8756. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8757. pos = req_pos&EQP_SHADOW_ACC;
  8758. if (pos == EQP_SHADOW_ACC)
  8759. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8760. }
  8761. if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8762. pos = (req_pos&EQP_SHADOW_ARMS);
  8763. if( pos == EQP_SHADOW_ARMS )
  8764. pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8765. }
  8766. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8767. //Update skill-block range database when weapon range changes. [Skotlex]
  8768. i = sd->equip_index[EQI_HAND_R];
  8769. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8770. flag = 1;
  8771. else
  8772. flag = id->range != sd->inventory_data[i]->range;
  8773. }
  8774. for(i=0;i<EQI_MAX;i++) {
  8775. if(pos & equip_bitmask[i]) {
  8776. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8777. pc_unequipitem(sd,sd->equip_index[i],2);
  8778. sd->equip_index[i] = n;
  8779. }
  8780. }
  8781. if(pos==EQP_AMMO) {
  8782. clif_arrowequip(sd,n);
  8783. clif_arrow_fail(sd,3);
  8784. }
  8785. else
  8786. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8787. sd->inventory.u.items_inventory[n].equip = pos;
  8788. if(pos & EQP_HAND_R) {
  8789. if(id)
  8790. sd->weapontype1 = id->look;
  8791. else
  8792. sd->weapontype1 = 0;
  8793. pc_calcweapontype(sd);
  8794. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8795. }
  8796. if(pos & EQP_HAND_L) {
  8797. if(id) {
  8798. if(id->type == IT_WEAPON) {
  8799. sd->status.shield = 0;
  8800. sd->weapontype2 = id->look;
  8801. }
  8802. else
  8803. if(id->type == IT_ARMOR) {
  8804. sd->status.shield = id->look;
  8805. sd->weapontype2 = 0;
  8806. }
  8807. }
  8808. else
  8809. sd->status.shield = sd->weapontype2 = 0;
  8810. pc_calcweapontype(sd);
  8811. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8812. }
  8813. if(pos & EQP_SHOES)
  8814. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8815. pc_set_costume_view(sd);
  8816. pc_checkallowskill(sd); //Check if status changes should be halted.
  8817. iflag = sd->npc_item_flag;
  8818. /* check for combos (MUST be before status_calc_pc) */
  8819. if( id->combos_count )
  8820. pc_checkcombo(sd,id);
  8821. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8822. ; //No cards
  8823. else {
  8824. for( i = 0; i < MAX_SLOTS; i++ ) {
  8825. struct item_data *data;
  8826. if (!sd->inventory.u.items_inventory[n].card[i])
  8827. continue;
  8828. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8829. if( data->combos_count )
  8830. pc_checkcombo(sd,data);
  8831. }
  8832. }
  8833. }
  8834. status_calc_pc(sd,SCO_NONE);
  8835. if (flag) //Update skill data
  8836. clif_skillinfoblock(sd);
  8837. //OnEquip script [Skotlex]
  8838. if (id) {
  8839. //only run the script if item isn't restricted
  8840. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8841. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8842. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8843. ; //No cards
  8844. else {
  8845. for( i = 0; i < MAX_SLOTS; i++ ) {
  8846. struct item_data *data;
  8847. if (!sd->inventory.u.items_inventory[n].card[i])
  8848. continue;
  8849. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8850. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8851. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8852. }
  8853. }
  8854. }
  8855. }
  8856. sd->npc_item_flag = iflag;
  8857. return true;
  8858. }
  8859. /*==========================================
  8860. * Called when attemting to unequip an item from player
  8861. * type:
  8862. * 0 - only unequip
  8863. * 1 - calculate status after unequipping
  8864. * 2 - force unequip
  8865. * return: false - fail; true - success
  8866. *------------------------------------------*/
  8867. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8868. int i, iflag;
  8869. bool status_calc = false;
  8870. nullpo_retr(false,sd);
  8871. if (n < 0 || n >= MAX_INVENTORY) {
  8872. clif_unequipitemack(sd,0,0,0);
  8873. return false;
  8874. }
  8875. if (!sd->inventory.u.items_inventory[n].equip) {
  8876. clif_unequipitemack(sd,n,0,0);
  8877. return false; //Nothing to unequip
  8878. }
  8879. // status change that makes player cannot unequip equipment
  8880. if (!(flag&2) && sd->sc.count &&
  8881. (sd->sc.data[SC_BERSERK] ||
  8882. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8883. sd->sc.data[SC__BLOODYLUST] ||
  8884. sd->sc.data[SC_KYOUGAKU] ||
  8885. (sd->sc.data[SC_PYROCLASTIC] &&
  8886. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8887. {
  8888. clif_unequipitemack(sd,n,0,0);
  8889. return false;
  8890. }
  8891. if (battle_config.battle_log)
  8892. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->inventory.u.items_inventory[n].equip);
  8893. if (!sd->inventory.u.items_inventory[n].equip) { //Nothing to unequip
  8894. clif_unequipitemack(sd, n, 0, 0);
  8895. return false;
  8896. }
  8897. for(i = 0; i < EQI_MAX; i++) {
  8898. if (sd->inventory.u.items_inventory[n].equip & equip_bitmask[i])
  8899. sd->equip_index[i] = -1;
  8900. }
  8901. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_R) {
  8902. sd->weapontype1 = 0;
  8903. sd->status.weapon = sd->weapontype2;
  8904. pc_calcweapontype(sd);
  8905. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8906. if( !battle_config.dancing_weaponswitch_fix )
  8907. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8908. }
  8909. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_L) {
  8910. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  8911. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  8912. sd->status.shield = sd->weapontype2 = 0;
  8913. pc_calcweapontype(sd);
  8914. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8915. }
  8916. if(sd->inventory.u.items_inventory[n].equip & EQP_SHOES)
  8917. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8918. clif_unequipitemack(sd,n,sd->inventory.u.items_inventory[n].equip,1);
  8919. pc_set_costume_view(sd);
  8920. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8921. // On weapon change (right and left hand)
  8922. if ((sd->inventory.u.items_inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8923. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8924. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8925. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8926. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8927. }
  8928. // On armor change
  8929. if (sd->inventory.u.items_inventory[n].equip & EQP_ARMOR) {
  8930. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8931. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8932. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8933. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8934. }
  8935. // On ammo change
  8936. if (sd->inventory_data[n]->type == IT_AMMO && (sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET || sd->inventory_data[n]->nameid != ITEMID_PURIFICATION_BULLET || sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET_))
  8937. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8938. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8939. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8940. sd->inventory.u.items_inventory[n].equip = 0;
  8941. iflag = sd->npc_item_flag;
  8942. /* check for combos (MUST be before status_calc_pc) */
  8943. if ( sd->inventory_data[n] ) {
  8944. if( sd->inventory_data[n]->combos_count ) {
  8945. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8946. status_calc = true;
  8947. }
  8948. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8949. ; //No cards
  8950. else {
  8951. for( i = 0; i < MAX_SLOTS; i++ ) {
  8952. struct item_data *data;
  8953. if (!sd->inventory.u.items_inventory[n].card[i])
  8954. continue;
  8955. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8956. if( data->combos_count ) {
  8957. if( pc_removecombo(sd,data) )
  8958. status_calc = true;
  8959. }
  8960. }
  8961. }
  8962. }
  8963. }
  8964. if(flag&1 || status_calc) {
  8965. pc_checkallowskill(sd);
  8966. status_calc_pc(sd,SCO_NONE);
  8967. }
  8968. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8969. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8970. //OnUnEquip script [Skotlex]
  8971. if (sd->inventory_data[n]) {
  8972. if (sd->inventory_data[n]->unequip_script)
  8973. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8974. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8975. ; //No cards
  8976. else {
  8977. for( i = 0; i < MAX_SLOTS; i++ ) {
  8978. struct item_data *data;
  8979. if (!sd->inventory.u.items_inventory[n].card[i])
  8980. continue;
  8981. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8982. if( data->unequip_script )
  8983. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8984. }
  8985. }
  8986. }
  8987. }
  8988. sd->npc_item_flag = iflag;
  8989. return true;
  8990. }
  8991. /*==========================================
  8992. * Checking if player (sd) has an invalid item
  8993. * and is unequiped on map load (item_noequip)
  8994. *------------------------------------------*/
  8995. void pc_checkitem(struct map_session_data *sd) {
  8996. int i, calc_flag = 0;
  8997. struct item it;
  8998. nullpo_retv(sd);
  8999. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9000. return;
  9001. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9002. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9003. it = sd->inventory.u.items_inventory[i];
  9004. if( it.nameid == 0 )
  9005. continue;
  9006. if( !it.equip )
  9007. continue;
  9008. if( it.equip&~pc_equippoint(sd,i) ) {
  9009. pc_unequipitem(sd, i, 2);
  9010. calc_flag = 1;
  9011. continue;
  9012. }
  9013. 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) ) {
  9014. pc_unequipitem(sd, i, 2);
  9015. calc_flag = 1;
  9016. continue;
  9017. }
  9018. }
  9019. if( calc_flag && sd->state.active ) {
  9020. pc_checkallowskill(sd);
  9021. status_calc_pc(sd,SCO_NONE);
  9022. }
  9023. }
  9024. /*==========================================
  9025. * Checks for unavailable items and removes them.
  9026. * @param sd: Player data
  9027. * @param type Forced check:
  9028. * 1 - Inventory
  9029. * 2 - Cart
  9030. * 4 - Storage
  9031. *------------------------------------------*/
  9032. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9033. {
  9034. int i;
  9035. unsigned short nameid;
  9036. char output[256];
  9037. nullpo_retv(sd);
  9038. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9039. for(i = 0; i < MAX_INVENTORY; i++) {
  9040. nameid = sd->inventory.u.items_inventory[i].nameid;
  9041. if (!nameid)
  9042. continue;
  9043. if (!itemdb_available(nameid)) {
  9044. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9045. clif_displaymessage(sd->fd, output);
  9046. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  9047. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9048. continue;
  9049. }
  9050. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9051. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9052. }
  9053. }
  9054. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9055. for(i = 0; i < MAX_CART; i++) {
  9056. nameid = sd->cart.u.items_cart[i].nameid;
  9057. if (!nameid)
  9058. continue;
  9059. if (!itemdb_available(nameid)) {
  9060. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9061. clif_displaymessage(sd->fd, output);
  9062. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  9063. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9064. continue;
  9065. }
  9066. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9067. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9068. }
  9069. }
  9070. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9071. for(i = 0; i < sd->storage.max_amount; i++) {
  9072. nameid = sd->storage.u.items_storage[i].nameid;
  9073. if (!nameid)
  9074. continue;
  9075. if (!itemdb_available(nameid)) {
  9076. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9077. clif_displaymessage(sd->fd, output);
  9078. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  9079. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9080. continue;
  9081. }
  9082. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9083. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9084. }
  9085. }
  9086. }
  9087. /*==========================================
  9088. * Update PVP rank for sd1 in cmp to sd2
  9089. *------------------------------------------*/
  9090. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9091. {
  9092. struct map_session_data *sd1,*sd2;
  9093. sd1=(struct map_session_data *)bl;
  9094. sd2=va_arg(ap,struct map_session_data *);
  9095. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9096. {// cannot register pvp rank for hidden GMs
  9097. return 0;
  9098. }
  9099. if( sd1->pvp_point > sd2->pvp_point )
  9100. sd2->pvp_rank++;
  9101. return 0;
  9102. }
  9103. /*==========================================
  9104. * Calculate new rank beetween all present players (map_foreachinallarea)
  9105. * and display result
  9106. *------------------------------------------*/
  9107. int pc_calc_pvprank(struct map_session_data *sd)
  9108. {
  9109. int old = sd->pvp_rank;
  9110. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9111. sd->pvp_rank=1;
  9112. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9113. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9114. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9115. return sd->pvp_rank;
  9116. }
  9117. /*==========================================
  9118. * Calculate next sd ranking calculation from config
  9119. *------------------------------------------*/
  9120. TIMER_FUNC(pc_calc_pvprank_timer){
  9121. struct map_session_data *sd;
  9122. sd=map_id2sd(id);
  9123. if(sd==NULL)
  9124. return 0;
  9125. sd->pvp_timer = INVALID_TIMER;
  9126. if( pc_isinvisible(sd) )
  9127. {// do not calculate the pvp rank for a hidden GM
  9128. return 0;
  9129. }
  9130. if( pc_calc_pvprank(sd) > 0 )
  9131. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9132. return 0;
  9133. }
  9134. /*==========================================
  9135. * Checking if sd is married
  9136. * Return:
  9137. * partner_id = yes
  9138. * 0 = no
  9139. *------------------------------------------*/
  9140. int pc_ismarried(struct map_session_data *sd)
  9141. {
  9142. if(sd == NULL)
  9143. return -1;
  9144. if(sd->status.partner_id > 0)
  9145. return sd->status.partner_id;
  9146. else
  9147. return 0;
  9148. }
  9149. /*==========================================
  9150. * Marry player sd to player dstsd
  9151. * Return:
  9152. * false = fail
  9153. * true = success
  9154. *------------------------------------------*/
  9155. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9156. {
  9157. if(sd == NULL || dstsd == NULL ||
  9158. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9159. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9160. return false;
  9161. sd->status.partner_id = dstsd->status.char_id;
  9162. dstsd->status.partner_id = sd->status.char_id;
  9163. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9164. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9165. return true;
  9166. }
  9167. /*==========================================
  9168. * Divorce sd from its partner
  9169. * Return:
  9170. * false = fail
  9171. * true = success
  9172. *------------------------------------------*/
  9173. bool pc_divorce(struct map_session_data *sd)
  9174. {
  9175. struct map_session_data *p_sd;
  9176. int i;
  9177. if( sd == NULL || !pc_ismarried(sd) )
  9178. return false;
  9179. if( !sd->status.partner_id )
  9180. return false; // Char is not married
  9181. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9182. { // Lets char server do the divorce
  9183. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9184. return false; // No char server connected
  9185. return true;
  9186. }
  9187. // Both players online, lets do the divorce manually
  9188. sd->status.partner_id = 0;
  9189. p_sd->status.partner_id = 0;
  9190. for( i = 0; i < MAX_INVENTORY; i++ )
  9191. {
  9192. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9193. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9194. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9195. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9196. }
  9197. clif_divorced(sd, p_sd->status.name);
  9198. clif_divorced(p_sd, sd->status.name);
  9199. return true;
  9200. }
  9201. /**
  9202. * Get the partner map_session_data of a player
  9203. * @param sd : the husband|wife session
  9204. * @return partner session or NULL
  9205. */
  9206. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9207. if (!sd || !pc_ismarried(sd))
  9208. return NULL;
  9209. return map_charid2sd(sd->status.partner_id);
  9210. }
  9211. /**
  9212. * Get the father map_session_data of a player
  9213. * @param sd : the baby session
  9214. * @return father session or NULL
  9215. */
  9216. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9217. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9218. return NULL;
  9219. return map_charid2sd(sd->status.father);
  9220. }
  9221. /**
  9222. * Get the mother map_session_data of a player
  9223. * @param sd : the baby session
  9224. * @return mother session or NULL
  9225. */
  9226. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9227. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9228. return NULL;
  9229. return map_charid2sd(sd->status.mother);
  9230. }
  9231. /*==========================================
  9232. * Get sd children charid. (Need to be married)
  9233. *------------------------------------------*/
  9234. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9235. {
  9236. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9237. // charid2sd returns NULL if not found
  9238. return NULL;
  9239. return map_charid2sd(sd->status.child);
  9240. }
  9241. /*==========================================
  9242. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9243. *------------------------------------------*/
  9244. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9245. {
  9246. int hp = 0, sp = 0;
  9247. if( pc_isdead(sd) )
  9248. return;
  9249. if (sd->hp_loss.value) {
  9250. sd->hp_loss.tick += diff_tick;
  9251. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9252. hp += sd->hp_loss.value;
  9253. sd->hp_loss.tick -= sd->hp_loss.rate;
  9254. }
  9255. if(hp >= sd->battle_status.hp)
  9256. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9257. }
  9258. if (sd->sp_loss.value) {
  9259. sd->sp_loss.tick += diff_tick;
  9260. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9261. sp += sd->sp_loss.value;
  9262. sd->sp_loss.tick -= sd->sp_loss.rate;
  9263. }
  9264. }
  9265. if (hp > 0 || sp > 0)
  9266. status_zap(&sd->bl, hp, sp);
  9267. }
  9268. //Character regen. Flag is used to know which types of regen can take place.
  9269. //&1: HP regen
  9270. //&2: SP regen
  9271. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9272. {
  9273. int hp = 0, sp = 0;
  9274. if (sd->hp_regen.value) {
  9275. sd->hp_regen.tick += diff_tick;
  9276. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9277. hp += sd->hp_regen.value;
  9278. sd->hp_regen.tick -= sd->hp_regen.rate;
  9279. }
  9280. }
  9281. if (sd->sp_regen.value) {
  9282. sd->sp_regen.tick += diff_tick;
  9283. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9284. sp += sd->sp_regen.value;
  9285. sd->sp_regen.tick -= sd->sp_regen.rate;
  9286. }
  9287. }
  9288. if (sd->percent_hp_regen.value) {
  9289. sd->percent_hp_regen.tick += diff_tick;
  9290. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9291. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9292. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9293. }
  9294. }
  9295. if (sd->percent_sp_regen.value) {
  9296. sd->percent_sp_regen.tick += diff_tick;
  9297. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9298. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9299. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9300. }
  9301. }
  9302. if (hp > 0 || sp > 0)
  9303. status_heal(&sd->bl, hp, sp, 0);
  9304. }
  9305. /*==========================================
  9306. * Memo player sd savepoint. (map,x,y)
  9307. *------------------------------------------*/
  9308. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9309. {
  9310. nullpo_retv(sd);
  9311. sd->status.save_point.map = mapindex;
  9312. sd->status.save_point.x = x;
  9313. sd->status.save_point.y = y;
  9314. }
  9315. /*==========================================
  9316. * Save 1 player data at autosave interval
  9317. *------------------------------------------*/
  9318. static TIMER_FUNC(pc_autosave){
  9319. int interval;
  9320. struct s_mapiterator* iter;
  9321. struct map_session_data* sd;
  9322. static int last_save_id = 0, save_flag = 0;
  9323. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9324. save_flag = 0;
  9325. else
  9326. save_flag = 1; //Noone was saved, so save first found char.
  9327. iter = mapit_getallusers();
  9328. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9329. {
  9330. if(sd->bl.id == last_save_id && save_flag != 1) {
  9331. save_flag = 1;
  9332. continue;
  9333. }
  9334. if(save_flag != 1) //Not our turn to save yet.
  9335. continue;
  9336. //Save char.
  9337. last_save_id = sd->bl.id;
  9338. save_flag = 2;
  9339. if (pc_isvip(sd)) // Check if we're still VIP
  9340. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9341. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9342. break;
  9343. }
  9344. mapit_free(iter);
  9345. interval = autosave_interval/(map_usercount()+1);
  9346. if(interval < minsave_interval)
  9347. interval = minsave_interval;
  9348. add_timer(gettick()+interval,pc_autosave,0,0);
  9349. return 0;
  9350. }
  9351. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9352. {
  9353. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9354. { //Night/day state does not match.
  9355. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9356. sd->state.night = night_flag;
  9357. return 1;
  9358. }
  9359. return 0;
  9360. }
  9361. /*================================================
  9362. * timer to do the day [Yor]
  9363. * data: 0 = called by timer, 1 = gmcommand/script
  9364. *------------------------------------------------*/
  9365. TIMER_FUNC(map_day_timer){
  9366. char tmp_soutput[1024];
  9367. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9368. return 0;
  9369. if (!night_flag)
  9370. return 0; //Already day.
  9371. night_flag = 0; // 0=day, 1=night [Yor]
  9372. map_foreachpc(pc_daynight_timer_sub);
  9373. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9374. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9375. return 0;
  9376. }
  9377. /*================================================
  9378. * timer to do the night [Yor]
  9379. * data: 0 = called by timer, 1 = gmcommand/script
  9380. *------------------------------------------------*/
  9381. TIMER_FUNC(map_night_timer){
  9382. char tmp_soutput[1024];
  9383. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9384. return 0;
  9385. if (night_flag)
  9386. return 0; //Already nigth.
  9387. night_flag = 1; // 0=day, 1=night [Yor]
  9388. map_foreachpc(pc_daynight_timer_sub);
  9389. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9390. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9391. return 0;
  9392. }
  9393. /**
  9394. * Attempt to stand up a player
  9395. * @param sd
  9396. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9397. * @return True if success, Fals if failed
  9398. */
  9399. bool pc_setstand(struct map_session_data *sd, bool force){
  9400. nullpo_ret(sd);
  9401. // Cannot stand yet
  9402. // TODO: Move to SCS_NOSTAND [Cydh]
  9403. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9404. return false;
  9405. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9406. clif_status_load(&sd->bl,EFST_SIT,0);
  9407. clif_standing(&sd->bl); //Inform area PC is standing
  9408. //Reset sitting tick.
  9409. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9410. if( pc_isdead( sd ) ){
  9411. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9412. clif_party_dead( sd );
  9413. }else{
  9414. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9415. }
  9416. return true;
  9417. }
  9418. /**
  9419. * Calculate Overheat value
  9420. * @param sd: Player data
  9421. * @param heat: Amount of Heat to adjust
  9422. **/
  9423. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9424. struct status_change_entry *sce = NULL;
  9425. int16 limit[] = { 150, 200, 280, 360, 450 };
  9426. uint16 skill_lv;
  9427. nullpo_retv(sd);
  9428. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9429. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9430. sce->val1 += heat;
  9431. sce->val1 = cap_value(sce->val1, 0, 1000);
  9432. if (sd->sc.data[SC_OVERHEAT])
  9433. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9434. if (sce->val1 > limit[skill_lv])
  9435. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9436. } else if (heat > 0)
  9437. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9438. }
  9439. /**
  9440. * Check if player is autolooting given itemID.
  9441. */
  9442. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9443. {
  9444. uint8 i = 0;
  9445. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9446. return true;
  9447. if (!sd->state.autolooting)
  9448. return false;
  9449. if (sd->state.autolooting)
  9450. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9451. return (i != AUTOLOOTITEM_SIZE);
  9452. }
  9453. /**
  9454. * Checks if player can use @/#command
  9455. * @param sd Player map session data
  9456. * @param command Command name without @/# and params
  9457. * @param type is it atcommand or charcommand
  9458. */
  9459. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9460. {
  9461. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9462. }
  9463. /**
  9464. * Checks if commands used by a player should be logged
  9465. * according to their group setting.
  9466. * @param sd Player map session data
  9467. */
  9468. bool pc_should_log_commands(struct map_session_data *sd)
  9469. {
  9470. return pc_group_should_log_commands(pc_get_group_id(sd));
  9471. }
  9472. /**
  9473. * Spirit Charm expiration timer.
  9474. * @see TimerFunc
  9475. */
  9476. static TIMER_FUNC(pc_spiritcharm_timer){
  9477. struct map_session_data *sd;
  9478. int i;
  9479. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9480. return 1;
  9481. if (sd->spiritcharm <= 0) {
  9482. 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);
  9483. sd->spiritcharm = 0;
  9484. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9485. return 0;
  9486. }
  9487. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9488. if (i == sd->spiritcharm) {
  9489. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9490. return 0;
  9491. }
  9492. sd->spiritcharm--;
  9493. if (i != sd->spiritcharm)
  9494. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9495. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9496. if (sd->spiritcharm <= 0)
  9497. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9498. clif_spiritcharm(sd);
  9499. return 0;
  9500. }
  9501. /**
  9502. * Adds a spirit charm.
  9503. * @param sd: Target character
  9504. * @param interval: Duration
  9505. * @param max: Maximum amount of charms to add
  9506. * @param type: Charm type (@see spirit_charm_types)
  9507. */
  9508. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9509. {
  9510. int tid, i;
  9511. nullpo_retv(sd);
  9512. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9513. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9514. if (max > MAX_SPIRITCHARM)
  9515. max = MAX_SPIRITCHARM;
  9516. if (sd->spiritcharm < 0)
  9517. sd->spiritcharm = 0;
  9518. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9519. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9520. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9521. sd->spiritcharm--;
  9522. if (sd->spiritcharm != 0)
  9523. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9524. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9525. }
  9526. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9527. 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);
  9528. if (i != sd->spiritcharm)
  9529. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9530. sd->spiritcharm_timer[i] = tid;
  9531. sd->spiritcharm++;
  9532. sd->spiritcharm_type = type;
  9533. clif_spiritcharm(sd);
  9534. }
  9535. /**
  9536. * Removes one or more spirit charms.
  9537. * @param sd: The target character
  9538. * @param count: Amount of charms to remove
  9539. * @param type: Type of charm to remove
  9540. */
  9541. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9542. {
  9543. int i;
  9544. nullpo_retv(sd);
  9545. if (sd->spiritcharm_type != type)
  9546. return;
  9547. if (sd->spiritcharm <= 0) {
  9548. sd->spiritcharm = 0;
  9549. return;
  9550. }
  9551. if (count <= 0)
  9552. return;
  9553. if (count > sd->spiritcharm)
  9554. count = sd->spiritcharm;
  9555. sd->spiritcharm -= count;
  9556. if (count > MAX_SPIRITCHARM)
  9557. count = MAX_SPIRITCHARM;
  9558. for (i = 0; i < count; i++) {
  9559. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9560. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9561. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9562. }
  9563. }
  9564. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9565. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9566. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9567. }
  9568. if (sd->spiritcharm <= 0)
  9569. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9570. clif_spiritcharm(sd);
  9571. }
  9572. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9573. /**
  9574. * Renewal EXP/Item Drop rate modifier based on level penalty
  9575. * @param level_diff: Monster and Player level difference
  9576. * @param mob_class: Monster class
  9577. * @param mode: Monster mode
  9578. * @param type: 1 - EXP, 2 - Item Drop
  9579. * @return Penalty rate
  9580. */
  9581. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9582. {
  9583. int rate = 100;
  9584. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9585. return rate;
  9586. if (level_diff < 0)
  9587. level_diff = MAX_LEVEL + (~level_diff + 1);
  9588. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9589. return rate;
  9590. return 100; // Penalty not found, return default
  9591. }
  9592. #endif
  9593. int pc_split_str(char *str,char **val,int num)
  9594. {
  9595. int i;
  9596. for (i=0; i<num && str; i++){
  9597. val[i] = str;
  9598. str = strchr(str,',');
  9599. if (str && i<num-1) //Do not remove a trailing comma.
  9600. *str++=0;
  9601. }
  9602. return i;
  9603. }
  9604. int pc_split_atoi(char* str, int* val, char sep, int max)
  9605. {
  9606. int i,j;
  9607. for (i=0; i<max; i++) {
  9608. if (!str) break;
  9609. val[i] = atoi(str);
  9610. str = strchr(str,sep);
  9611. if (str)
  9612. *str++=0;
  9613. }
  9614. //Zero up the remaining.
  9615. for(j=i; j < max; j++)
  9616. val[j] = 0;
  9617. return i;
  9618. }
  9619. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9620. {
  9621. static int warning=0;
  9622. int i,j;
  9623. for (i=0; i<max; i++) {
  9624. double f;
  9625. if (!str) break;
  9626. f = atof(str);
  9627. if (f < 0)
  9628. val[i] = 0;
  9629. else if (f > UINT_MAX) {
  9630. val[i] = UINT_MAX;
  9631. if (!warning) {
  9632. warning = 1;
  9633. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9634. }
  9635. } else
  9636. val[i] = (unsigned int)f;
  9637. str = strchr(str,sep);
  9638. if (str)
  9639. *str++=0;
  9640. }
  9641. //Zero up the remaining.
  9642. for(j=i; j < max; j++)
  9643. val[j] = 0;
  9644. return i;
  9645. }
  9646. /*==========================================
  9647. * sub DB reading.
  9648. * Function used to read skill_tree.txt
  9649. *------------------------------------------*/
  9650. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9651. {
  9652. uint32 baselv, joblv, baselv_max, joblv_max;
  9653. uint16 skill_id, skill_lv, skill_lv_max;
  9654. int idx, class_;
  9655. unsigned int i, offset, skill_idx;
  9656. class_ = atoi(fields[0]);
  9657. skill_id = (uint16)atoi(fields[1]);
  9658. skill_lv = (uint16)atoi(fields[2]);
  9659. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9660. baselv = (uint32)atoi(fields[3]);
  9661. joblv = (uint32)atoi(fields[4]);
  9662. offset = 5;
  9663. }
  9664. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9665. baselv = joblv = 0;
  9666. offset = 3;
  9667. }
  9668. else {
  9669. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9670. return false;
  9671. }
  9672. if(!pcdb_checkid(class_))
  9673. {
  9674. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9675. return false;
  9676. }
  9677. idx = pc_class2idx(class_);
  9678. if (!skill_get_index(skill_id)) {
  9679. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9680. return false;
  9681. }
  9682. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9683. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  9684. skill_lv = skill_lv_max;
  9685. }
  9686. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9687. ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
  9688. baselv = baselv_max;
  9689. }
  9690. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9691. ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
  9692. joblv = joblv_max;
  9693. }
  9694. //This is to avoid adding two lines for the same skill. [Skotlex]
  9695. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
  9696. if( skill_idx == MAX_SKILL_TREE )
  9697. {
  9698. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
  9699. return false;
  9700. }
  9701. else if(skill_tree[idx][skill_idx].skill_id)
  9702. {
  9703. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9704. }
  9705. skill_tree[idx][skill_idx].skill_id = skill_id;
  9706. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9707. skill_tree[idx][skill_idx].baselv = baselv;
  9708. skill_tree[idx][skill_idx].joblv = joblv;
  9709. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9710. {
  9711. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9712. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9713. if (skill_id == 0) {
  9714. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9715. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9716. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9717. }
  9718. continue;
  9719. }
  9720. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9721. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9722. return false;
  9723. }
  9724. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9725. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  9726. skill_lv = skill_lv_max;
  9727. }
  9728. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9729. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9730. }
  9731. return true;
  9732. }
  9733. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9734. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9735. {
  9736. int type, class_, diff;
  9737. type = atoi(fields[0]); //1=experience, 2=item drop
  9738. class_ = atoi(fields[1]);
  9739. diff = atoi(fields[2]);
  9740. if( type != 1 && type != 2 ){
  9741. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9742. return false;
  9743. }
  9744. if( !CHK_CLASS(class_) ){
  9745. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9746. return false;
  9747. }
  9748. diff = min(diff, MAX_LEVEL);
  9749. if( diff < 0 )
  9750. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9751. level_penalty[type][class_][diff] = atoi(fields[3]);
  9752. return true;
  9753. }
  9754. #endif
  9755. /** [Cydh]
  9756. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9757. * @param level Base level of player
  9758. * @param class_ Job ID @see enum e_job
  9759. * @return base_hp
  9760. */
  9761. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9762. double base_hp;
  9763. uint16 i, idx = pc_class2idx(class_);
  9764. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9765. #ifndef RENEWAL
  9766. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9767. #endif
  9768. for (i = 2; i <= level; i++)
  9769. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9770. if (class_ == JOB_SUMMONER)
  9771. base_hp += floor((base_hp / 2) + 0.5);
  9772. return (unsigned int)base_hp;
  9773. }
  9774. /** [Playtester]
  9775. * Calculates base sp of player.
  9776. * @param level Base level of player
  9777. * @param class_ Job ID @see enum e_job
  9778. * @return base_sp
  9779. */
  9780. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9781. double base_sp;
  9782. uint16 idx = pc_class2idx(class_);
  9783. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9784. switch (class_) {
  9785. case JOB_NINJA:
  9786. if (level >= 10)
  9787. base_sp -= 22;
  9788. else
  9789. base_sp = 11 + 3*level;
  9790. break;
  9791. case JOB_GUNSLINGER:
  9792. if (level > 10)
  9793. base_sp -= 18;
  9794. else
  9795. base_sp = 9 + 3*level;
  9796. break;
  9797. case JOB_SUMMONER:
  9798. base_sp -= floor(base_sp / 2);
  9799. break;
  9800. }
  9801. return (unsigned int)base_sp;
  9802. }
  9803. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9804. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9805. int idx, class_;
  9806. unsigned int i;
  9807. class_ = atoi(fields[0]);
  9808. if (!pcdb_checkid(class_)) {
  9809. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9810. return false;
  9811. }
  9812. idx = pc_class2idx(class_);
  9813. job_info[idx].max_weight_base = atoi(fields[1]);
  9814. job_info[idx].hp_factor = atoi(fields[2]);
  9815. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9816. job_info[idx].sp_factor = atoi(fields[4]);
  9817. #ifdef RENEWAL_ASPD
  9818. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9819. #else
  9820. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9821. #endif
  9822. {
  9823. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9824. }
  9825. return true;
  9826. }
  9827. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9828. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9829. {
  9830. int idx, class_, i;
  9831. class_ = atoi(fields[0]);
  9832. if(!pcdb_checkid(class_))
  9833. {
  9834. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9835. return false;
  9836. }
  9837. idx = pc_class2idx(class_);
  9838. for(i = 1; i < columns; i++)
  9839. {
  9840. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9841. }
  9842. return true;
  9843. }
  9844. //Reading job_exp.txt line
  9845. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9846. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9847. {
  9848. int idx, i, type;
  9849. int job_id,job_count,jobs[CLASS_COUNT];
  9850. unsigned int ui, maxlvl;
  9851. maxlvl = atoi(fields[0]);
  9852. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9853. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9854. return false;
  9855. }
  9856. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9857. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9858. return false;
  9859. }
  9860. type = atoi(fields[2]);
  9861. if(type < 0 || type > 1){
  9862. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9863. return false;
  9864. }
  9865. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9866. if (job_count < 1)
  9867. return false;
  9868. job_id = jobs[0];
  9869. if(!pcdb_checkid(job_id)){
  9870. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9871. return false;
  9872. }
  9873. idx = pc_class2idx(job_id);
  9874. job_info[idx].max_level[type] = maxlvl;
  9875. for(i=0; i<maxlvl; i++)
  9876. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9877. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9878. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9879. //should look like this:
  9880. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9881. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9882. job_info[idx].max_level[type]--;
  9883. if (job_info[idx].max_level[type] < maxlvl) {
  9884. 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]);
  9885. ShowInfo("Filling the missing values with the last exp entry.\n");
  9886. //Fill the requested values with the last entry.
  9887. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9888. for (; ui+2 < maxlvl; ui++)
  9889. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9890. job_info[idx].max_level[type] = maxlvl;
  9891. }
  9892. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9893. for (i = 1; i < job_count; i++) {
  9894. job_id = jobs[i];
  9895. if (!pcdb_checkid(job_id)) {
  9896. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9897. continue;
  9898. }
  9899. idx = pc_class2idx(job_id);
  9900. 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]));
  9901. job_info[idx].max_level[type] = maxlvl;
  9902. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9903. }
  9904. return true;
  9905. }
  9906. /**
  9907. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9908. * startlvl,endlvl,class,type,values...
  9909. */
  9910. #ifdef HP_SP_TABLES
  9911. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9912. {
  9913. int i, startlvl, endlvl;
  9914. int job_count,jobs[CLASS_COUNT];
  9915. short type;
  9916. startlvl = atoi(fields[0]);
  9917. if(startlvl<1){
  9918. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9919. return false;
  9920. }
  9921. endlvl = atoi(fields[1]);
  9922. if(endlvl<1 || endlvl<startlvl){
  9923. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9924. return false;
  9925. }
  9926. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9927. 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);
  9928. return false;
  9929. }
  9930. type = atoi(fields[3]);
  9931. if(type < 0 || type > 1){
  9932. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9933. return false;
  9934. }
  9935. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9936. if (job_count < 1)
  9937. return false;
  9938. for (i = 0; i < job_count; i++) {
  9939. int idx, job_id = jobs[i], use_endlvl;
  9940. if (!pcdb_checkid(job_id)) {
  9941. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9942. return false;
  9943. }
  9944. idx = pc_class2idx(job_id);
  9945. if (startlvl > job_info[idx].max_level[0]) {
  9946. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9947. return false;
  9948. }
  9949. //Just read until available max level for this job, don't use MAX_LEVEL!
  9950. use_endlvl = endlvl;
  9951. if (use_endlvl > job_info[idx].max_level[0])
  9952. use_endlvl = job_info[idx].max_level[0];
  9953. if(type == 0) { //hp type
  9954. uint16 j;
  9955. for(j = 0; j < use_endlvl; j++) {
  9956. if (atoi(fields[j+4])) {
  9957. uint16 lvl_idx = startlvl-1+j;
  9958. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9959. //Tells if this HP is lower than previous level (but not for 99->100)
  9960. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9961. 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",
  9962. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9963. }
  9964. }
  9965. }
  9966. else { //sp type
  9967. uint16 j;
  9968. for(j = 0; j < use_endlvl; j++) {
  9969. if (atoi(fields[j+4])) {
  9970. uint16 lvl_idx = startlvl-1+j;
  9971. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9972. //Tells if this SP is lower than previous level (but not for 99->100)
  9973. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9974. 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",
  9975. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9976. }
  9977. }
  9978. }
  9979. }
  9980. return true;
  9981. }
  9982. #endif
  9983. /** [Cydh]
  9984. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9985. */
  9986. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9987. {
  9988. int idx, class_;
  9989. uint16 str, agi, vit, int_, dex, luk;
  9990. script_get_constant(trim(fields[0]),&class_);
  9991. if ((idx = pc_class2idx(class_)) < 0) {
  9992. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9993. return false;
  9994. }
  9995. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9996. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9997. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9998. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9999. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10000. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10001. job_info[idx].max_param.str = str;
  10002. job_info[idx].max_param.agi = agi;
  10003. job_info[idx].max_param.vit = vit;
  10004. job_info[idx].max_param.int_ = int_;
  10005. job_info[idx].max_param.dex = dex;
  10006. job_info[idx].max_param.luk = luk;
  10007. return true;
  10008. }
  10009. /**
  10010. * Read job_noenter_map.txt
  10011. **/
  10012. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10013. int idx, class_ = -1;
  10014. if (ISDIGIT(str[0][0])) {
  10015. class_ = atoi(str[0]);
  10016. } else {
  10017. if (!script_get_constant(str[0], &class_)) {
  10018. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10019. return false;
  10020. }
  10021. }
  10022. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10023. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  10024. return false;
  10025. }
  10026. job_info[idx].noenter_map.zone = atoi(str[1]);
  10027. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10028. return true;
  10029. }
  10030. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10031. int i=1;
  10032. char line[24000]; //FIXME this seem too big
  10033. FILE *fp;
  10034. sprintf(line, "%s/statpoint.txt", basedir);
  10035. fp=fopen(line,"r");
  10036. if(fp == NULL){
  10037. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10038. return max(last_s,i);
  10039. } else {
  10040. int entries=0;
  10041. while(fgets(line, sizeof(line), fp))
  10042. {
  10043. int stat;
  10044. trim(line);
  10045. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10046. continue;
  10047. if ((stat=strtoul(line,NULL,10))<0)
  10048. stat=0;
  10049. if (i > MAX_LEVEL)
  10050. break;
  10051. statp[i]=stat;
  10052. i++;
  10053. entries++;
  10054. }
  10055. fclose(fp);
  10056. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10057. }
  10058. return max(last_s,i);
  10059. }
  10060. /*==========================================
  10061. * pc DB reading.
  10062. * job_exp.txt - required experience values
  10063. * skill_tree.txt - skill tree for every class
  10064. * attr_fix.txt - elemental adjustment table
  10065. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10066. * job_db2.txt - job,stats bonuses/lvl
  10067. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10068. *------------------------------------------*/
  10069. void pc_readdb(void) {
  10070. int i, k, s = 1;
  10071. const char* dbsubpath[] = {
  10072. "",
  10073. "/" DBIMPORT,
  10074. //add other path here
  10075. };
  10076. //reset
  10077. memset(job_info,0,sizeof(job_info)); // job_info table
  10078. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10079. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10080. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10081. for( k=1; k < 3; k++ ){ // fill in the blanks
  10082. int j;
  10083. for( j = 0; j < CLASS_ALL; j++ ){
  10084. int tmp = 0;
  10085. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10086. if( i == MAX_LEVEL+1 )
  10087. tmp = level_penalty[k][j][0];// reset
  10088. if( level_penalty[k][j][i] > 0 )
  10089. tmp = level_penalty[k][j][i];
  10090. else
  10091. level_penalty[k][j][i] = tmp;
  10092. }
  10093. }
  10094. }
  10095. #endif
  10096. // reset then read statspoint
  10097. memset(statp,0,sizeof(statp));
  10098. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10099. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10100. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10101. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10102. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10103. if(i==0) {
  10104. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10105. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10106. }
  10107. else {
  10108. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10109. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10110. }
  10111. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10112. if (i == 0)
  10113. #ifdef RENEWAL_ASPD
  10114. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10115. #else
  10116. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10117. #endif
  10118. else
  10119. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10120. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10121. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10122. #ifdef HP_SP_TABLES
  10123. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
  10124. #endif
  10125. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10126. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10127. aFree(dbsubpath1);
  10128. aFree(dbsubpath2);
  10129. }
  10130. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10131. memset(skill_tree, 0, sizeof(skill_tree));
  10132. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
  10133. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
  10134. // generate the remaining parts of the db if necessary
  10135. k = battle_config.use_statpoint_table; //save setting
  10136. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10137. statp[0] = 45; // seed value
  10138. for (; s <= MAX_LEVEL; s++)
  10139. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10140. battle_config.use_statpoint_table = k; //restore setting
  10141. //Checking if all class have their data
  10142. for (i = 0; i < JOB_MAX; i++) {
  10143. int idx;
  10144. uint16 j;
  10145. if (!pcdb_checkid(i))
  10146. continue;
  10147. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10148. continue; //Classes that do not need exp tables.
  10149. idx = pc_class2idx(i);
  10150. if (!job_info[idx].max_level[0])
  10151. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10152. if (!job_info[idx].max_level[1])
  10153. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10154. //Init and checking the empty value of Base HP/SP [Cydh]
  10155. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10156. if (job_info[idx].base_hp[j] == 0)
  10157. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10158. if (job_info[idx].base_sp[j] == 0)
  10159. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10160. }
  10161. }
  10162. }
  10163. // Read MOTD on startup. [Valaris]
  10164. int pc_read_motd(void)
  10165. {
  10166. FILE* fp;
  10167. // clear old MOTD
  10168. memset(motd_text, 0, sizeof(motd_text));
  10169. // read current MOTD
  10170. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10171. {
  10172. unsigned int entries = 0;
  10173. char buf[CHAT_SIZE_MAX];
  10174. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10175. {
  10176. unsigned int lines = 0;
  10177. size_t len;
  10178. lines++;
  10179. if( buf[0] == '/' && buf[1] == '/' )
  10180. continue;
  10181. len = strlen(buf);
  10182. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10183. len--;
  10184. if( len ) {
  10185. char * ptr;
  10186. buf[len] = 0;
  10187. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10188. 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);
  10189. }
  10190. else {// empty line
  10191. buf[0] = ' ';
  10192. buf[1] = 0;
  10193. }
  10194. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10195. entries++;
  10196. }
  10197. fclose(fp);
  10198. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10199. }
  10200. else
  10201. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10202. return 0;
  10203. }
  10204. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10205. int i,cursor = 0;
  10206. struct item_cd* cd = NULL;
  10207. if( load ) {
  10208. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10209. // no item cooldown is associated with this character
  10210. return;
  10211. }
  10212. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10213. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10214. sd->item_delay[cursor].tick = cd->tick[i];
  10215. sd->item_delay[cursor].nameid = cd->nameid[i];
  10216. cursor++;
  10217. }
  10218. }
  10219. idb_remove(itemcd_db,sd->status.char_id);
  10220. } else {
  10221. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10222. // create a new skill cooldown object for map storage
  10223. CREATE( cd, struct item_cd, 1 );
  10224. idb_put( itemcd_db, sd->status.char_id, cd );
  10225. }
  10226. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10227. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10228. cd->tick[cursor] = sd->item_delay[i].tick;
  10229. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10230. cursor++;
  10231. }
  10232. }
  10233. }
  10234. return;
  10235. }
  10236. /**
  10237. * Add item delay to player's item delay data
  10238. * @param sd Player
  10239. * @param id Item data
  10240. * @param tick Current tick
  10241. * @param n Item index in inventory
  10242. * @return 0: No delay, can consume item.
  10243. * 1: Has delay, cancel consumption.
  10244. **/
  10245. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10246. int i;
  10247. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10248. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10249. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10250. if( i < MAX_ITEMDELAYS ) {
  10251. if( sd->item_delay[i].nameid ) {// found
  10252. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10253. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10254. char e_msg[CHAT_SIZE_MAX];
  10255. if( e_tick > 99 )
  10256. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10257. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10258. else
  10259. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10260. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10261. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10262. return 1; // Delay has not expired yet
  10263. }
  10264. } else {// not yet used item (all slots are initially empty)
  10265. sd->item_delay[i].nameid = id->nameid;
  10266. }
  10267. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10268. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10269. } else {// should not happen
  10270. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10271. }
  10272. //clean up used delays so we can give room for more
  10273. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10274. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10275. sd->item_delay[i].tick = 0;
  10276. sd->item_delay[i].nameid = 0;
  10277. }
  10278. }
  10279. return 0;
  10280. }
  10281. /**
  10282. * Check if player has delay to reuse item
  10283. * @param sd Player
  10284. * @param id Item data
  10285. * @param tick Current tick
  10286. * @param n Item index in inventory
  10287. * @return 0: No delay, can consume item.
  10288. * 1: Has delay, cancel consumption.
  10289. **/
  10290. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10291. struct status_change *sc = NULL;
  10292. nullpo_retr(0, sd);
  10293. nullpo_retr(0, id);
  10294. // Do normal delay assignment
  10295. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10296. return pc_itemcd_add(sd, id, tick, n);
  10297. // Send reply of delay remains
  10298. if (sc->data[id->delay_sc]) {
  10299. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10300. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10301. return 1;
  10302. }
  10303. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10304. return 0;
  10305. }
  10306. /**
  10307. * Clear the dmglog data from player
  10308. * @param sd
  10309. * @param md
  10310. **/
  10311. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10312. {
  10313. uint8 i;
  10314. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10315. if (i < DAMAGELOG_SIZE) {
  10316. md->dmglog[i].id = 0;
  10317. md->dmglog[i].dmg = 0;
  10318. md->dmglog[i].flag = 0;
  10319. }
  10320. }
  10321. /**
  10322. * Add log to player's dmglog
  10323. * @param sd
  10324. * @param id Monster's GID
  10325. **/
  10326. void pc_damage_log_add(struct map_session_data *sd, int id)
  10327. {
  10328. uint8 i = 0;
  10329. if (!sd || !id)
  10330. return;
  10331. //Only store new data, don't need to renew the old one with same id
  10332. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10333. if (i < DAMAGELOG_SIZE_PC)
  10334. return;
  10335. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10336. if (sd->dmglog[i] == 0) {
  10337. sd->dmglog[i] = id;
  10338. return;
  10339. }
  10340. }
  10341. }
  10342. /**
  10343. * Clear dmglog data from player
  10344. * @param sd
  10345. * @param id Monster's id
  10346. **/
  10347. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10348. {
  10349. uint8 i;
  10350. struct mob_data *md = NULL;
  10351. if (!sd)
  10352. return;
  10353. if (!id) {
  10354. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10355. if( !sd->dmglog[i] ) //skip the empty value
  10356. continue;
  10357. if ((md = map_id2md(sd->dmglog[i])))
  10358. pc_clear_log_damage_sub(sd->status.char_id,md);
  10359. sd->dmglog[i] = 0;
  10360. }
  10361. }
  10362. else {
  10363. if ((md = map_id2md(id)))
  10364. pc_clear_log_damage_sub(sd->status.char_id,md);
  10365. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10366. if (i < DAMAGELOG_SIZE_PC)
  10367. sd->dmglog[i] = 0;
  10368. }
  10369. }
  10370. /**
  10371. * Status change data arrived from char-server
  10372. * @param sd: Player data
  10373. */
  10374. void pc_scdata_received(struct map_session_data *sd) {
  10375. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10376. clif_weight_limit( sd );
  10377. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10378. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10379. }
  10380. sd->state.pc_loaded = true;
  10381. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10382. sd->state.connect_new = 1;
  10383. clif_parse_LoadEndAck(sd->fd, sd);
  10384. }
  10385. if (pc_iscarton(sd)) {
  10386. sd->cart_weight_max = 0; // Force a client refesh
  10387. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10388. }
  10389. }
  10390. /**
  10391. * Check player account expiration time and rental item expirations
  10392. * @param sd: Player data
  10393. */
  10394. void pc_check_expiration(struct map_session_data *sd) {
  10395. #ifndef ENABLE_SC_SAVING
  10396. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10397. #endif
  10398. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10399. time_t exp_time = sd->expiration_time;
  10400. char tmpstr[1024];
  10401. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10402. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10403. pc_expire_check(sd);
  10404. }
  10405. }
  10406. TIMER_FUNC(pc_expiration_timer){
  10407. struct map_session_data *sd = map_id2sd(id);
  10408. if( !sd ) return 0;
  10409. sd->expiration_tid = INVALID_TIMER;
  10410. if( sd->fd )
  10411. clif_authfail_fd(sd->fd,10);
  10412. map_quit(sd);
  10413. return 0;
  10414. }
  10415. TIMER_FUNC(pc_autotrade_timer){
  10416. struct map_session_data *sd = map_id2sd(id);
  10417. if (!sd)
  10418. return 0;
  10419. sd->autotrade_tid = INVALID_TIMER;
  10420. if (sd->state.autotrade&2)
  10421. vending_reopen(sd);
  10422. if (sd->state.autotrade&4)
  10423. buyingstore_reopen(sd);
  10424. if (!sd->vender_id && !sd->buyer_id) {
  10425. sd->state.autotrade = 0;
  10426. map_quit(sd);
  10427. }
  10428. return 0;
  10429. }
  10430. /* this timer exists only when a character with a expire timer > 24h is online */
  10431. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10432. TIMER_FUNC(pc_global_expiration_timer){
  10433. struct s_mapiterator* iter;
  10434. struct map_session_data* sd;
  10435. iter = mapit_getallusers();
  10436. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10437. if( sd->expiration_time )
  10438. pc_expire_check(sd);
  10439. mapit_free(iter);
  10440. return 0;
  10441. }
  10442. void pc_expire_check(struct map_session_data *sd) {
  10443. /* ongoing timer */
  10444. if( sd->expiration_tid != INVALID_TIMER )
  10445. return;
  10446. /* not within the next 24h, enable the global check */
  10447. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10448. /* global check not running, enable */
  10449. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10450. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10451. return;
  10452. }
  10453. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10454. }
  10455. /**
  10456. * Deposit some money to bank
  10457. * @param sd
  10458. * @param money Amount of money to deposit
  10459. **/
  10460. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10461. unsigned int limit_check = money + sd->bank_vault;
  10462. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10463. return BDA_OVERFLOW;
  10464. } else if ( money > sd->status.zeny ) {
  10465. return BDA_NO_MONEY;
  10466. }
  10467. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10468. return BDA_NO_MONEY;
  10469. sd->bank_vault += money;
  10470. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10471. if( save_settings&CHARSAVE_BANK )
  10472. chrif_save(sd, CSAVE_NORMAL);
  10473. return BDA_SUCCESS;
  10474. }
  10475. /**
  10476. * Withdraw money from bank
  10477. * @param sd
  10478. * @param money Amount of money that will be withdrawn
  10479. **/
  10480. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10481. unsigned int limit_check = money + sd->status.zeny;
  10482. if( money <= 0 ) {
  10483. return BWA_UNKNOWN_ERROR;
  10484. } else if ( money > sd->bank_vault ) {
  10485. return BWA_NO_MONEY;
  10486. } else if ( limit_check > MAX_ZENY ) {
  10487. /* no official response for this scenario exists. */
  10488. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10489. return BWA_UNKNOWN_ERROR;
  10490. }
  10491. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10492. return BWA_NO_MONEY;
  10493. sd->bank_vault -= money;
  10494. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10495. if( save_settings&CHARSAVE_BANK )
  10496. chrif_save(sd, CSAVE_NORMAL);
  10497. return BWA_SUCCESS;
  10498. }
  10499. /**
  10500. * Clear Crimson Marker data from caster
  10501. * @param sd: Player
  10502. **/
  10503. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10504. uint8 i;
  10505. if (!sd)
  10506. return;
  10507. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10508. struct block_list *bl = NULL;
  10509. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10510. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10511. sd->c_marker[i] = 0;
  10512. }
  10513. }
  10514. /**
  10515. * Show version to player
  10516. * @param sd: Player
  10517. **/
  10518. void pc_show_version(struct map_session_data *sd) {
  10519. const char* svn = get_svn_revision();
  10520. char buf[CHAT_SIZE_MAX];
  10521. if( svn[0] != UNKNOWN_VERSION )
  10522. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10523. else {
  10524. const char* git = get_git_hash();
  10525. if( git[0] != UNKNOWN_VERSION )
  10526. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10527. else
  10528. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10529. }
  10530. clif_displaymessage(sd->fd,buf);
  10531. }
  10532. /**
  10533. * Run bonus_script on player
  10534. * @param sd
  10535. * @author [Cydh]
  10536. **/
  10537. void pc_bonus_script(struct map_session_data *sd) {
  10538. int now = gettick();
  10539. struct linkdb_node *node = NULL, *next = NULL;
  10540. if (!sd || !(node = sd->bonus_script.head))
  10541. return;
  10542. while (node) {
  10543. struct s_bonus_script_entry *entry = NULL;
  10544. next = node->next;
  10545. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10546. // Only start timer for new bonus_script
  10547. if (entry->tid == INVALID_TIMER) {
  10548. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10549. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10550. entry->tick += now;
  10551. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10552. }
  10553. if (entry->script)
  10554. run_script(entry->script, 0, sd->bl.id, 0);
  10555. else
  10556. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10557. }
  10558. node = next;
  10559. }
  10560. }
  10561. /**
  10562. * Add bonus_script to player
  10563. * @param sd Player
  10564. * @param script_str Script string
  10565. * @param dur Duration in ms
  10566. * @param icon EFST
  10567. * @param flag Flags @see enum e_bonus_script_flags
  10568. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10569. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10570. * @author [Cydh]
  10571. **/
  10572. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum efst_types icon, uint16 flag, uint8 type) {
  10573. struct script_code *script = NULL;
  10574. struct linkdb_node *node = NULL;
  10575. struct s_bonus_script_entry *entry = NULL;
  10576. if (!sd)
  10577. return NULL;
  10578. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10579. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10580. return NULL;
  10581. }
  10582. // Duplication checks
  10583. if ((node = sd->bonus_script.head)) {
  10584. while (node) {
  10585. entry = (struct s_bonus_script_entry *)node->data;
  10586. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10587. int newdur = gettick() + dur;
  10588. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10589. settick_timer(entry->tid, newdur);
  10590. script_free_code(script);
  10591. return NULL;
  10592. }
  10593. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10594. break;
  10595. else { // No duplicate bonus
  10596. script_free_code(script);
  10597. return NULL;
  10598. }
  10599. }
  10600. node = node->next;
  10601. }
  10602. }
  10603. CREATE(entry, struct s_bonus_script_entry, 1);
  10604. entry->script_buf = StringBuf_Malloc();
  10605. StringBuf_AppendStr(entry->script_buf, script_str);
  10606. entry->tid = INVALID_TIMER;
  10607. entry->flag = flag;
  10608. entry->icon = icon;
  10609. entry->tick = dur; // Use duration first, on run change to expire time
  10610. entry->type = type;
  10611. entry->script = script;
  10612. sd->bonus_script.count++;
  10613. return entry;
  10614. }
  10615. /**
  10616. * Remove bonus_script data from player
  10617. * @param sd: Target player
  10618. * @param list: Bonus script entry from player
  10619. * @author [Cydh]
  10620. **/
  10621. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10622. if (entry->tid != INVALID_TIMER)
  10623. delete_timer(entry->tid, pc_bonus_script_timer);
  10624. if (entry->script)
  10625. script_free_code(entry->script);
  10626. if (entry->script_buf)
  10627. StringBuf_Free(entry->script_buf);
  10628. if (sd) {
  10629. if (entry->icon != EFST_BLANK)
  10630. clif_status_load(&sd->bl, entry->icon, 0);
  10631. if (sd->bonus_script.count > 0)
  10632. sd->bonus_script.count--;
  10633. }
  10634. aFree(entry);
  10635. }
  10636. /**
  10637. * Do final process if no entry left
  10638. * @param sd
  10639. **/
  10640. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10641. if (sd->bonus_script.count == 0) {
  10642. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10643. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10644. linkdb_final(&sd->bonus_script.head);
  10645. }
  10646. }
  10647. /**
  10648. * Timer for bonus_script
  10649. * @param tid
  10650. * @param tick
  10651. * @param id
  10652. * @param data
  10653. * @author [Cydh]
  10654. **/
  10655. TIMER_FUNC(pc_bonus_script_timer){
  10656. struct map_session_data *sd;
  10657. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10658. sd = map_id2sd(id);
  10659. if (!sd) {
  10660. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10661. return 0;
  10662. }
  10663. if (tid == INVALID_TIMER)
  10664. return 0;
  10665. if (!sd->bonus_script.head || entry == NULL) {
  10666. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10667. return 0;
  10668. }
  10669. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10670. pc_bonus_script_free_entry(sd, entry);
  10671. pc_bonus_script_check_final(sd);
  10672. status_calc_pc(sd,SCO_NONE);
  10673. return 0;
  10674. }
  10675. /**
  10676. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10677. * @param sd: Target player
  10678. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10679. * @author [Cydh]
  10680. **/
  10681. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10682. struct linkdb_node *node = NULL;
  10683. uint16 count = 0;
  10684. if (!sd || !(node = sd->bonus_script.head))
  10685. return;
  10686. while (node) {
  10687. struct linkdb_node *next = node->next;
  10688. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10689. if (entry && (
  10690. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10691. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10692. (flag&entry->flag) || // Matched flag
  10693. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10694. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10695. )
  10696. )
  10697. {
  10698. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10699. pc_bonus_script_free_entry(sd, entry);
  10700. count++;
  10701. }
  10702. node = next;
  10703. }
  10704. pc_bonus_script_check_final(sd);
  10705. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10706. status_calc_pc(sd,SCO_NONE);
  10707. }
  10708. /** [Cydh]
  10709. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10710. * @param sd: Target player
  10711. */
  10712. void pc_cell_basilica(struct map_session_data *sd) {
  10713. nullpo_retv(sd);
  10714. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10715. if (sd->sc.data[SC_BASILICA])
  10716. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10717. }
  10718. else if (!sd->sc.data[SC_BASILICA])
  10719. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10720. }
  10721. /** [Cydh]
  10722. * Get maximum specified parameter for specified class
  10723. * @param class_: sd->class
  10724. * @param sex: sd->status.sex
  10725. * @param flag: parameter will be checked
  10726. * @return max_param
  10727. */
  10728. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10729. int idx = -1, class_ = sd->class_;
  10730. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10731. short max_param = 0;
  10732. switch (param) {
  10733. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10734. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10735. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10736. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10737. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10738. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10739. }
  10740. if (max_param > 0)
  10741. return max_param;
  10742. }
  10743. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10744. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10745. ((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)) :
  10746. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10747. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10748. }
  10749. /**
  10750. * Get max ASPD for player based on Class
  10751. * @param sd Player
  10752. * @return ASPD
  10753. */
  10754. short pc_maxaspd(struct map_session_data *sd) {
  10755. nullpo_ret(sd);
  10756. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10757. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10758. battle_config.max_aspd ));
  10759. }
  10760. /**
  10761. * Calculates total item-group related bonuses for the given item
  10762. * @param sd Player
  10763. * @param nameid Item ID
  10764. * @return Heal rate
  10765. **/
  10766. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10767. short bonus = 0;
  10768. uint8 i;
  10769. if (!sd->itemgrouphealrate_count)
  10770. return bonus;
  10771. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10772. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10773. struct s_item_group_db *group = NULL;
  10774. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10775. continue;
  10776. for (j = 0; j < group->random[0].data_qty; j++) {
  10777. if (group->random[0].data[j].nameid == nameid) {
  10778. bonus += sd->itemgrouphealrate[i]->rate;
  10779. break;
  10780. }
  10781. }
  10782. }
  10783. return bonus;
  10784. }
  10785. /**
  10786. * Calculates total item-group related bonuses for the given item group
  10787. * @param sd Player
  10788. * @param group_id Item Group ID
  10789. * @return Heal rate
  10790. **/
  10791. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10792. short bonus = 0;
  10793. uint8 i;
  10794. if (!sd->itemgrouphealrate_count)
  10795. return bonus;
  10796. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10797. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10798. return sd->itemgrouphealrate[i]->rate;
  10799. }
  10800. return bonus;
  10801. }
  10802. /**
  10803. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10804. * @param eqi Item EQI of enum equip_index
  10805. * @param *equip_index Player's equip_index[]
  10806. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10807. * @return True if item in same inventory index, False if doesn't
  10808. */
  10809. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10810. if (index < 0 || index >= MAX_INVENTORY)
  10811. return true;
  10812. // Dual weapon checks
  10813. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10814. return true;
  10815. // Headgear with Mid & Low location
  10816. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10817. return true;
  10818. // Headgear with Top & Mid or Low location
  10819. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10820. return true;
  10821. // Headgear with Mid & Low location
  10822. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10823. return true;
  10824. // Headgear with Top & Mid or Low location
  10825. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10826. return true;
  10827. return false;
  10828. }
  10829. /**
  10830. * Generate Unique item ID for player
  10831. * @param sd : Player
  10832. * @return A generated Unique item ID
  10833. */
  10834. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10835. nullpo_ret(sd);
  10836. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10837. }
  10838. /**
  10839. * Validating skill from player after logged on
  10840. * @param sd
  10841. **/
  10842. void pc_validate_skill(struct map_session_data *sd) {
  10843. if (sd) {
  10844. uint16 i = 0, count = 0;
  10845. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10846. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10847. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10848. for (i = 0; i < MAX_SKILL; i++) {
  10849. uint16 idx = 0;
  10850. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10851. continue;
  10852. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10853. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10854. count++;
  10855. }
  10856. else
  10857. 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);
  10858. }
  10859. }
  10860. }
  10861. /**
  10862. * Toggle to remember if the questinfo is displayed yet or not.
  10863. * @param qi_display Display flag
  10864. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10865. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10866. **/
  10867. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10868. if (show) {
  10869. // Check if need to be displayed
  10870. if ((*qi_display) != 1) {
  10871. (*qi_display) = 1;
  10872. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10873. }
  10874. }
  10875. else {
  10876. // Check if need to be hide
  10877. if ((*qi_display) != 0) {
  10878. (*qi_display) = 0;
  10879. #if PACKETVER >= 20120410
  10880. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10881. #else
  10882. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10883. #endif
  10884. }
  10885. }
  10886. }
  10887. /**
  10888. * Show available NPC Quest / Event Icon Check [Kisuka]
  10889. * @param sd Player
  10890. **/
  10891. void pc_show_questinfo(struct map_session_data *sd) {
  10892. #if PACKETVER >= 20090218
  10893. struct questinfo *qi = NULL;
  10894. unsigned short i;
  10895. uint8 j;
  10896. int8 mystate = 0;
  10897. bool failed = false;
  10898. nullpo_retv(sd);
  10899. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10900. return;
  10901. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10902. if (!mapdata->qi_count || !mapdata->qi_data)
  10903. return;
  10904. if (mapdata->qi_count != sd->qi_count)
  10905. return; // init was not called yet
  10906. for(i = 0; i < mapdata->qi_count; i++) {
  10907. qi = &mapdata->qi_data[i];
  10908. if (!qi)
  10909. continue;
  10910. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10911. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10912. continue;
  10913. }
  10914. // Level range checks
  10915. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10916. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10917. continue;
  10918. }
  10919. // Quest requirements
  10920. if (qi->req_count) {
  10921. failed = false;
  10922. for (j = 0; j < qi->req_count; j++) {
  10923. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10924. mystate = mystate + (mystate < 1);
  10925. if (mystate != qi->req[j].state) {
  10926. failed = true;
  10927. break;
  10928. }
  10929. }
  10930. if (failed) {
  10931. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10932. continue;
  10933. }
  10934. }
  10935. // Job requirements
  10936. if (qi->jobid_count) {
  10937. failed = true;
  10938. for (j = 0; j < qi->jobid_count; j++) {
  10939. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10940. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10941. failed = false;
  10942. break;
  10943. }
  10944. }
  10945. if (!failed)
  10946. continue;
  10947. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10948. }
  10949. else {
  10950. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10951. }
  10952. }
  10953. #endif
  10954. }
  10955. /**
  10956. * Reinit the questinfo for player when changing map
  10957. * @param sd Player
  10958. **/
  10959. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10960. #if PACKETVER >= 20090218
  10961. nullpo_retv(sd);
  10962. if (sd->qi_display) {
  10963. aFree(sd->qi_display);
  10964. sd->qi_display = NULL;
  10965. }
  10966. sd->qi_count = 0;
  10967. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10968. return;
  10969. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10970. if (!mapdata->qi_count || !mapdata->qi_data)
  10971. return;
  10972. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
  10973. #endif
  10974. }
  10975. /**
  10976. * Check if a job is allowed to enter the map
  10977. * @param jobid Job ID see enum e_job or sd->status.class_
  10978. * @param m ID -an index- for direct indexing map[] array
  10979. * @return 1 if job is allowed, 0 otherwise
  10980. **/
  10981. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10982. uint16 idx = 0;
  10983. // Map is other map server.
  10984. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10985. if (m < 0)
  10986. return true;
  10987. struct map_data *mapdata = map_getmapdata(m);
  10988. if (!mapdata->cell)
  10989. return false;
  10990. if (!pcdb_checkid(jobid))
  10991. return false;
  10992. idx = pc_class2idx(jobid);
  10993. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10994. return true;
  10995. if ((!map_flag_vs2(m) && job_info[idx].noenter_map.zone&1) || // Normal
  10996. (map_getmapflag(m, MF_PVP) && job_info[idx].noenter_map.zone&2) || // PVP
  10997. (map_flag_gvg2_no_te(m) && job_info[idx].noenter_map.zone&4) || // GVG
  10998. (map_getmapflag(m, MF_BATTLEGROUND) && job_info[idx].noenter_map.zone&8) || // Battleground
  10999. (map_flag_gvg2_te(m) && job_info[idx].noenter_map.zone&16) || // WOE:TE
  11000. (map_getmapflag(m, MF_RESTRICTED) && job_info[idx].noenter_map.zone&(8*mapdata->zone)) // Zone restriction
  11001. )
  11002. return false;
  11003. return true;
  11004. }
  11005. /**
  11006. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11007. * @param sd
  11008. **/
  11009. void pc_set_costume_view(struct map_session_data *sd) {
  11010. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11011. struct item_data *id = NULL;
  11012. nullpo_retv(sd);
  11013. head_low = sd->status.head_bottom;
  11014. head_mid = sd->status.head_mid;
  11015. head_top = sd->status.head_top;
  11016. robe = sd->status.robe;
  11017. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11018. //Added check to prevent sending the same look on multiple slots ->
  11019. //causes client to redraw item on top of itself. (suggested by Lupus)
  11020. // Normal headgear checks
  11021. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11022. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11023. sd->status.head_bottom = id->look;
  11024. else
  11025. sd->status.head_bottom = 0;
  11026. }
  11027. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11028. if (!(id->equip&(EQP_HEAD_TOP)))
  11029. sd->status.head_mid = id->look;
  11030. else
  11031. sd->status.head_mid = 0;
  11032. }
  11033. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11034. sd->status.head_top = id->look;
  11035. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11036. sd->status.robe = id->look;
  11037. // Costumes check
  11038. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11039. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11040. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11041. sd->status.head_bottom = id->look;
  11042. else
  11043. sd->status.head_bottom = 0;
  11044. }
  11045. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11046. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11047. sd->status.head_mid = id->look;
  11048. else
  11049. sd->status.head_mid = 0;
  11050. }
  11051. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11052. sd->status.head_top = id->look;
  11053. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11054. sd->status.robe = id->look;
  11055. }
  11056. if (sd->setlook_head_bottom)
  11057. sd->status.head_bottom = sd->setlook_head_bottom;
  11058. if (sd->setlook_head_mid)
  11059. sd->status.head_mid = sd->setlook_head_mid;
  11060. if (sd->setlook_head_top)
  11061. sd->status.head_top = sd->setlook_head_top;
  11062. if (sd->setlook_robe)
  11063. sd->status.robe = sd->setlook_robe;
  11064. if (head_low != sd->status.head_bottom)
  11065. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11066. if (head_mid != sd->status.head_mid)
  11067. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11068. if (head_top != sd->status.head_top)
  11069. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11070. if (robe != sd->status.robe)
  11071. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11072. }
  11073. struct s_attendance_period* pc_attendance_period(){
  11074. uint32 date = date_get(DT_YYYYMMDD);
  11075. for( struct s_attendance_period& period : attendance_periods ){
  11076. if( period.start <= date && period.end >= date ){
  11077. return &period;
  11078. }
  11079. }
  11080. return nullptr;
  11081. }
  11082. bool pc_attendance_enabled(){
  11083. // Check if the attendance feature is disabled
  11084. if( !battle_config.feature_attendance ){
  11085. return false;
  11086. }
  11087. // Check if there is a running attendance period
  11088. return pc_attendance_period() != nullptr;
  11089. }
  11090. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11091. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11092. }
  11093. int32 pc_attendance_counter( struct map_session_data* sd ){
  11094. struct s_attendance_period* period = pc_attendance_period();
  11095. // No running attendance period
  11096. if( period == nullptr ){
  11097. return 0;
  11098. }
  11099. // Get the counter for the current period
  11100. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11101. // Check if we have a remaining counter from a previous period
  11102. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11103. // Reset the counter to zero
  11104. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11105. return 0;
  11106. }
  11107. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11108. }
  11109. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11110. // If the user's group does not have the permission
  11111. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11112. return;
  11113. }
  11114. // Check if the attendance feature is disabled
  11115. if( !pc_attendance_enabled() ){
  11116. return;
  11117. }
  11118. // Check if the user already got his reward today
  11119. if( pc_attendance_rewarded_today( sd ) ){
  11120. return;
  11121. }
  11122. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11123. attendance_counter += 1;
  11124. struct s_attendance_period* period = pc_attendance_period();
  11125. if( period == nullptr ){
  11126. return;
  11127. }
  11128. if( period->rewards.size() < attendance_counter ){
  11129. return;
  11130. }
  11131. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11132. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11133. if( save_settings&CHARSAVE_ATTENDANCE )
  11134. chrif_save(sd, CSAVE_NORMAL);
  11135. struct s_attendance_reward& reward = period->rewards.at( attendance_counter - 1 );
  11136. struct mail_message msg;
  11137. memset( &msg, 0, sizeof( struct mail_message ) );
  11138. msg.dest_id = sd->status.char_id;
  11139. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11140. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11141. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11142. msg.item[0].nameid = reward.item_id;
  11143. msg.item[0].amount = reward.amount;
  11144. msg.item[0].identify = 1;
  11145. msg.status = MAIL_NEW;
  11146. msg.type = MAIL_INBOX_NORMAL;
  11147. msg.timestamp = time(NULL);
  11148. intif_Mail_send(0, &msg);
  11149. clif_attendence_response( sd, attendance_counter );
  11150. }
  11151. void pc_attendance_load( std::string path ){
  11152. YAML::Node root;
  11153. try{
  11154. root = YAML::LoadFile( path );
  11155. }catch( ... ){
  11156. ShowError( "pc_attendance_load: Failed to read attendance configuration file \"%s\".\n", path.c_str() );
  11157. return;
  11158. }
  11159. if( root["Attendance"] ){
  11160. YAML::Node attendance = root["Attendance"];
  11161. for( const auto &periodNode : attendance ){
  11162. if( !periodNode["Start"].IsDefined() ){
  11163. ShowError( "pc_attendance_load: Missing \"Start\" for period in line %d.\n", periodNode.Mark().line );
  11164. continue;
  11165. }
  11166. YAML::Node startNode = periodNode["Start"];
  11167. if( !periodNode["End"].IsDefined() ){
  11168. ShowError( "pc_attendance_load: Missing \"End\" for period in line %d.\n", periodNode.Mark().line );
  11169. continue;
  11170. }
  11171. YAML::Node endNode = periodNode["End"];
  11172. if( !periodNode["Rewards"].IsDefined() ){
  11173. ShowError( "pc_attendance_load: Missing \"Rewards\" for period in line %d.\n", periodNode.Mark().line );
  11174. continue;
  11175. }
  11176. YAML::Node rewardsNode = periodNode["Rewards"];
  11177. uint32 start = startNode.as<uint32>();
  11178. uint32 end = endNode.as<uint32>();
  11179. // If the period is outdated already, we do not even bother parsing
  11180. if( end < date_get( DT_YYYYMMDD ) ){
  11181. continue;
  11182. }
  11183. // Collision detection
  11184. bool collision = false;
  11185. for( struct s_attendance_period& period : attendance_periods ){
  11186. // Check if start is inside another period
  11187. if( period.start <= start && start <= period.end ){
  11188. ShowError( "pc_attendance_load: period start %u intersects with period %u-%u.\n", start, period.start, period.end );
  11189. collision = true;
  11190. break;
  11191. }
  11192. // Check if end is inside another period
  11193. if( period.start <= end && end <= period.end ){
  11194. ShowError( "pc_attendance_load: period end %u intersects with period %u-%u.\n", start, period.start, period.end );
  11195. collision = true;
  11196. break;
  11197. }
  11198. }
  11199. if( collision ){
  11200. continue;
  11201. }
  11202. struct s_attendance_period period;
  11203. period.start = start;
  11204. period.end = end;
  11205. for( const auto& rewardNode : rewardsNode ){
  11206. if( !rewardNode["Day"].IsDefined() ){
  11207. ShowError( "pc_attendance_load: No day defined for node in line %d.\n", rewardNode.Mark().line );
  11208. continue;
  11209. }
  11210. uint32 day = rewardNode["Day"].as<uint32>();
  11211. if( !rewardNode["ItemId"].IsDefined() ){
  11212. ShowError( "pc_attendance_load: No reward defined for day %d.\n", day );
  11213. continue;
  11214. }
  11215. YAML::Node itemNode = rewardNode["ItemId"];
  11216. uint16 item_id = itemNode.as<uint16>();
  11217. if( item_id == 0 || !itemdb_exists( item_id ) ){
  11218. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day );
  11219. continue;
  11220. }
  11221. uint16 amount;
  11222. if( rewardNode["Amount"] ){
  11223. amount = rewardNode["Amount"].as<uint16>();
  11224. if( amount == 0 ){
  11225. ShowError( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day );
  11226. amount = 1;
  11227. }else if( amount > MAX_AMOUNT ){
  11228. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day, MAX_AMOUNT );
  11229. amount = MAX_AMOUNT;
  11230. }
  11231. }else{
  11232. amount = 1;
  11233. }
  11234. struct s_attendance_reward* reward = &period.rewards[day - 1];
  11235. reward->item_id = item_id;
  11236. reward->amount = amount;
  11237. }
  11238. bool missing_day = false;
  11239. for( int day = 0; day < period.rewards.size(); day++ ){
  11240. if( !util::map_exists( period.rewards, day ) ){
  11241. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  11242. missing_day = true;
  11243. break;
  11244. }
  11245. }
  11246. if( missing_day ){
  11247. continue;
  11248. }
  11249. attendance_periods.push_back( period );
  11250. }
  11251. }
  11252. }
  11253. void pc_read_attendance(){
  11254. char path[1024];
  11255. sprintf( path, "%s/%sattendance.yml", db_path, DBPATH );
  11256. pc_attendance_load( path );
  11257. sprintf( path, "%s/%s/attendance.yml", db_path, DBIMPORT );
  11258. pc_attendance_load( path );
  11259. }
  11260. /*==========================================
  11261. * pc Init/Terminate
  11262. *------------------------------------------*/
  11263. void do_final_pc(void) {
  11264. db_destroy(itemcd_db);
  11265. do_final_pc_groups();
  11266. ers_destroy(pc_sc_display_ers);
  11267. ers_destroy(pc_itemgrouphealrate_ers);
  11268. ers_destroy(num_reg_ers);
  11269. ers_destroy(str_reg_ers);
  11270. attendance_periods.clear();
  11271. }
  11272. void do_init_pc(void) {
  11273. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11274. pc_readdb();
  11275. pc_read_motd(); // Read MOTD [Valaris]
  11276. pc_read_attendance();
  11277. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11278. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11279. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11280. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11281. add_timer_func_list(pc_autosave, "pc_autosave");
  11282. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11283. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11284. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11285. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11286. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11287. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11288. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11289. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11290. // 0=day, 1=night [Yor]
  11291. night_flag = battle_config.night_at_start ? 1 : 0;
  11292. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11293. int day_duration = battle_config.day_duration;
  11294. int night_duration = battle_config.night_duration;
  11295. // add night/day timer [Yor]
  11296. add_timer_func_list(map_day_timer, "map_day_timer");
  11297. add_timer_func_list(map_night_timer, "map_night_timer");
  11298. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11299. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11300. }
  11301. do_init_pc_groups();
  11302. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11303. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.cpp:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  11304. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11305. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11306. ers_chunk_size(pc_sc_display_ers, 150);
  11307. ers_chunk_size(num_reg_ers, 300);
  11308. ers_chunk_size(str_reg_ers, 50);
  11309. }