pc.c 379 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h" // get_svn_revision()
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/random.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/socket.h" // session[]
  10. #include "../common/strlib.h" // safestrncpy()
  11. #include "../common/timer.h"
  12. #include "../common/utils.h"
  13. #include "../common/mmo.h" //NAME_LENGTH
  14. #include "atcommand.h" // get_atcommand_level()
  15. #include "map.h"
  16. #include "battle.h" // battle_config
  17. #include "battleground.h"
  18. #include "channel.h"
  19. #include "chat.h"
  20. #include "chrif.h"
  21. #include "date.h" // is_day_of_*()
  22. #include "duel.h"
  23. #include "intif.h"
  24. #include "homunculus.h"
  25. #include "instance.h"
  26. #include "mercenary.h"
  27. #include "elemental.h"
  28. #include "pet.h" // pet_unlocktarget()
  29. #include "party.h" // party_search()
  30. #include "storage.h"
  31. #include "quest.h"
  32. #include <stdlib.h>
  33. #include <math.h>
  34. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  35. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  36. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  37. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  38. static unsigned int statp[MAX_LEVEL+1];
  39. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  40. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  41. #endif
  42. // h-files are for declarations, not for implementations... [Shinomori]
  43. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  44. // timer for night.day implementation
  45. int day_timer_tid = INVALID_TIMER;
  46. int night_timer_tid = INVALID_TIMER;
  47. struct eri *pc_sc_display_ers = NULL;
  48. struct eri *pc_itemgrouphealrate_ers = NULL;
  49. int pc_expiration_tid = INVALID_TIMER;
  50. struct fame_list smith_fame_list[MAX_FAME_LIST];
  51. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  52. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  53. #define MOTD_LINE_SIZE 128
  54. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  55. /**
  56. * Translation table from athena equip index to aegis bitmask
  57. */
  58. unsigned int equip_bitmask[EQI_MAX] = {
  59. EQP_ACC_L, // EQI_ACC_L
  60. EQP_ACC_R, // EQI_ACC_R
  61. EQP_SHOES, // EQI_SHOES
  62. EQP_GARMENT, // EQI_GARMENT
  63. EQP_HEAD_LOW, // EQI_HEAD_LOW
  64. EQP_HEAD_MID, // EQI_HEAD_MID
  65. EQP_HEAD_TOP, // EQI_HEAD_TOP
  66. EQP_ARMOR, // EQI_ARMOR
  67. EQP_HAND_L, // EQI_HAND_L
  68. EQP_HAND_R, // EQI_HAND_R
  69. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  70. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  71. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  72. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  73. EQP_AMMO, // EQI_AMMO
  74. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  75. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  76. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  77. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  78. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  79. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  80. };
  81. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  82. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  83. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  84. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  85. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  86. };
  87. /**
  88. * Item Cool Down Delay Saving
  89. * Struct item_cd is not a member of struct map_session_data
  90. * to keep cooldowns in memory between player log-ins.
  91. * All cooldowns are reset when server is restarted.
  92. **/
  93. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  94. struct item_cd {
  95. unsigned int tick[MAX_ITEMDELAYS]; //tick
  96. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  97. };
  98. /**
  99. * Converts a class to its array index for CLASS_COUNT defined arrays.
  100. * Note that it does not do a validity check for speed purposes, where parsing
  101. * player input make sure to use a pcdb_checkid first!
  102. * @param class_ Job ID see enum e_job
  103. * @return Class Index
  104. */
  105. int pc_class2idx(int class_) {
  106. if (class_ >= JOB_NOVICE_HIGH)
  107. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  108. return class_;
  109. }
  110. /**
  111. * Get player's group ID
  112. * @param sd
  113. * @return Group ID
  114. */
  115. inline int pc_get_group_id(struct map_session_data *sd) {
  116. return sd->group_id;
  117. }
  118. /** Get player's group Level
  119. * @param sd
  120. * @return Group Level
  121. */
  122. inline int pc_get_group_level(struct map_session_data *sd) {
  123. return sd->group_level;
  124. }
  125. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  126. {
  127. struct map_session_data *sd;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if(sd->invincible_timer != tid){
  131. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  132. return 0;
  133. }
  134. sd->invincible_timer = INVALID_TIMER;
  135. skill_unit_move(&sd->bl,tick,1);
  136. return 0;
  137. }
  138. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  139. nullpo_retv(sd);
  140. if( sd->invincible_timer != INVALID_TIMER )
  141. delete_timer(sd->invincible_timer,pc_invincible_timer);
  142. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  143. }
  144. void pc_delinvincibletimer(struct map_session_data* sd)
  145. {
  146. nullpo_retv(sd);
  147. if( sd->invincible_timer != INVALID_TIMER )
  148. {
  149. delete_timer(sd->invincible_timer,pc_invincible_timer);
  150. sd->invincible_timer = INVALID_TIMER;
  151. skill_unit_move(&sd->bl,gettick(),1);
  152. }
  153. }
  154. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  155. {
  156. struct map_session_data *sd;
  157. int i;
  158. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  159. return 1;
  160. if( sd->spiritball <= 0 )
  161. {
  162. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  163. sd->spiritball = 0;
  164. return 0;
  165. }
  166. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  167. if( i == sd->spiritball )
  168. {
  169. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  170. return 0;
  171. }
  172. sd->spiritball--;
  173. if( i != sd->spiritball )
  174. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  175. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  176. clif_spiritball(&sd->bl);
  177. return 0;
  178. }
  179. /**
  180. * Adds a spiritball to player for 'interval' ms
  181. * @param sd
  182. * @param interval
  183. * @param max
  184. */
  185. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  186. {
  187. int tid;
  188. uint8 i;
  189. nullpo_retv(sd);
  190. if(max > MAX_SPIRITBALL)
  191. max = MAX_SPIRITBALL;
  192. if(sd->spiritball < 0)
  193. sd->spiritball = 0;
  194. if( sd->spiritball && sd->spiritball >= max )
  195. {
  196. if(sd->spirit_timer[0] != INVALID_TIMER)
  197. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  198. sd->spiritball--;
  199. if( sd->spiritball != 0 )
  200. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  201. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  202. }
  203. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  204. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  205. if( i != sd->spiritball )
  206. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  207. sd->spirit_timer[i] = tid;
  208. sd->spiritball++;
  209. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  210. clif_millenniumshield(&sd->bl,sd->spiritball);
  211. else
  212. clif_spiritball(&sd->bl);
  213. }
  214. /**
  215. * Removes number of spiritball from player
  216. * @param sd
  217. * @param count
  218. * @param type 1 = doesn't give client effect
  219. */
  220. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  221. {
  222. uint8 i;
  223. nullpo_retv(sd);
  224. if(sd->spiritball <= 0) {
  225. sd->spiritball = 0;
  226. return;
  227. }
  228. if(count == 0)
  229. return;
  230. if(count > sd->spiritball)
  231. count = sd->spiritball;
  232. sd->spiritball -= count;
  233. if(count > MAX_SPIRITBALL)
  234. count = MAX_SPIRITBALL;
  235. for(i=0;i<count;i++) {
  236. if(sd->spirit_timer[i] != INVALID_TIMER) {
  237. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  238. sd->spirit_timer[i] = INVALID_TIMER;
  239. }
  240. }
  241. for(i=count;i<MAX_SPIRITBALL;i++) {
  242. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  243. sd->spirit_timer[i] = INVALID_TIMER;
  244. }
  245. if(!type) {
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. }
  252. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  253. int *c, *b_sd;
  254. struct block_list *src;
  255. struct map_session_data *tsd;
  256. struct status_change *sc;
  257. nullpo_ret(bl);
  258. nullpo_ret(tsd = (struct map_session_data*)bl);
  259. nullpo_ret(src = va_arg(ap,struct block_list *));
  260. c = va_arg(ap,int *);
  261. b_sd = va_arg(ap, int *);
  262. if(pc_isdead(tsd))
  263. return 0;
  264. sc = status_get_sc(bl);
  265. if( sc && sc->data[SC_BANDING] )
  266. {
  267. b_sd[(*c)++] = tsd->bl.id;
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  273. int c;
  274. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  275. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  276. int range = skill_get_splash(LG_BANDING,skill_lv);
  277. nullpo_ret(sd);
  278. c = 0;
  279. memset(b_sd, 0, sizeof(b_sd));
  280. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  281. if( c < 1 ) //just recalc status no need to recalc hp
  282. { // No more Royal Guards in Banding found.
  283. struct status_change *sc;
  284. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  285. {
  286. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  287. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  288. }
  289. return 0;
  290. }
  291. //Add yourself
  292. hp = status_get_hp(&sd->bl);
  293. i++;
  294. // Get total HP of all Royal Guards in party.
  295. for( j = 0; j < i; j++ ){
  296. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  297. if( bsd != NULL )
  298. hp += status_get_hp(&bsd->bl);
  299. }
  300. // Set average HP.
  301. hp = hp / i;
  302. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  303. for( j = 0; j < i; j++ )
  304. {
  305. int tmp_hp=0;
  306. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  307. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  308. extra_hp += tmp_hp;
  309. tmp_qty++;
  310. }
  311. }
  312. if( extra_hp > 0 && tmp_qty > 0 )
  313. hp += extra_hp / tmp_qty;
  314. for( j = 0; j < i; j++ ){
  315. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  316. if( bsd != NULL )
  317. {
  318. struct status_change *sc;
  319. status_set_hp(&bsd->bl,hp,0); // Set hp
  320. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  321. {
  322. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  323. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  324. }
  325. }
  326. }
  327. return c;
  328. }
  329. /**
  330. * Increases a player's fame points and displays a notice to him
  331. * @param sd Player
  332. * @param count Fame point
  333. */
  334. void pc_addfame(struct map_session_data *sd,int count)
  335. {
  336. enum e_rank ranktype;
  337. nullpo_retv(sd);
  338. sd->status.fame += count;
  339. if(sd->status.fame > MAX_FAME)
  340. sd->status.fame = MAX_FAME;
  341. switch(sd->class_&MAPID_UPPERMASK){
  342. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  343. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  344. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  345. default:
  346. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  347. return;
  348. }
  349. clif_update_rankingpoint(sd,ranktype,count);
  350. chrif_updatefamelist(sd);
  351. }
  352. /**
  353. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  354. * @param sd
  355. * @param job Job use enum e_mapid
  356. * @return Rank
  357. */
  358. unsigned char pc_famerank(uint32 char_id, int job)
  359. {
  360. uint8 i;
  361. switch(job){
  362. case MAPID_BLACKSMITH: // Blacksmith
  363. for(i = 0; i < MAX_FAME_LIST; i++){
  364. if(smith_fame_list[i].id == char_id)
  365. return i + 1;
  366. }
  367. break;
  368. case MAPID_ALCHEMIST: // Alchemist
  369. for(i = 0; i < MAX_FAME_LIST; i++){
  370. if(chemist_fame_list[i].id == char_id)
  371. return i + 1;
  372. }
  373. break;
  374. case MAPID_TAEKWON: // Taekwon
  375. for(i = 0; i < MAX_FAME_LIST; i++){
  376. if(taekwon_fame_list[i].id == char_id)
  377. return i + 1;
  378. }
  379. break;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * Restart player's HP & SP value
  385. * @param sd
  386. * @param type Restart type: 1 - Normal Resurection
  387. */
  388. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  389. struct status_data *status, *b_status;
  390. nullpo_retv(sd);
  391. b_status = &sd->base_status;
  392. status = &sd->battle_status;
  393. if (type&1) { //Normal resurrection
  394. status->hp = 1; //Otherwise status_heal may fail if dead.
  395. status_heal(&sd->bl, b_status->hp, 0, 1);
  396. if( status->sp < b_status->sp )
  397. status_set_sp(&sd->bl, b_status->sp, 1);
  398. } else { //Just for saving on the char-server (with values as if respawned)
  399. sd->status.hp = b_status->hp;
  400. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  401. }
  402. }
  403. /*==========================================
  404. Rental System
  405. *------------------------------------------*/
  406. /**
  407. * Ends a rental and removes the item/effect
  408. * @param tid: Tick ID
  409. * @param tick: Timer
  410. * @param id: Timer ID
  411. * @param data: Data
  412. * @return false - failure, true - success
  413. */
  414. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  415. {
  416. struct map_session_data *sd = map_id2sd(id);
  417. if( sd == NULL )
  418. return 0;
  419. if( tid != sd->rental_timer ) {
  420. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  421. return 0;
  422. }
  423. pc_inventory_rentals(sd);
  424. return 1;
  425. }
  426. /**
  427. * Removes the rental timer from the player
  428. * @param sd: Player data
  429. */
  430. void pc_inventory_rental_clear(struct map_session_data *sd)
  431. {
  432. if( sd->rental_timer != INVALID_TIMER ) {
  433. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  434. sd->rental_timer = INVALID_TIMER;
  435. }
  436. }
  437. /**
  438. * Check for items in the player's inventory that are rental type
  439. * @param sd: Player data
  440. */
  441. void pc_inventory_rentals(struct map_session_data *sd)
  442. {
  443. int i, c = 0;
  444. unsigned int next_tick = UINT_MAX;
  445. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  446. if( sd->status.inventory[i].nameid == 0 )
  447. continue; // Nothing here
  448. if( sd->status.inventory[i].expire_time == 0 )
  449. continue;
  450. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  451. if (sd->inventory_data[i]->unequip_script)
  452. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  453. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  454. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  455. } else {
  456. unsigned int expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL));
  457. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)expire_tick);
  458. next_tick = umin(expire_tick, next_tick);
  459. c++;
  460. }
  461. }
  462. 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
  463. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  464. else
  465. sd->rental_timer = INVALID_TIMER;
  466. }
  467. /**
  468. * Add a rental item to the player and adjusts the rental timer appropriately
  469. * @param sd: Player data
  470. * @param seconds: Rental time
  471. */
  472. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  473. {
  474. unsigned int tick = seconds * 1000;
  475. if( sd == NULL )
  476. return;
  477. if( sd->rental_timer != INVALID_TIMER ) {
  478. const struct TimerData * td;
  479. td = get_timer(sd->rental_timer);
  480. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  481. pc_inventory_rental_clear(sd);
  482. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  483. }
  484. } else
  485. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  486. }
  487. /**
  488. * Determines if player can give / drop / trade / vend items
  489. */
  490. bool pc_can_give_items(struct map_session_data *sd)
  491. {
  492. return pc_has_permission(sd, PC_PERM_TRADE);
  493. }
  494. /**
  495. * Determines if player can give / drop / trade / vend bounded items
  496. */
  497. bool pc_can_give_bounded_items(struct map_session_data *sd)
  498. {
  499. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  500. }
  501. /*==========================================
  502. * Prepares character for saving.
  503. * @param sd
  504. *------------------------------------------*/
  505. void pc_makesavestatus(struct map_session_data *sd) {
  506. nullpo_retv(sd);
  507. if(!battle_config.save_clothcolor)
  508. sd->status.clothes_color = 0;
  509. // Since this is currently not officially released,
  510. // its best to have a forced option to not save body styles.
  511. if(!battle_config.save_body_style)
  512. sd->status.body = 0;
  513. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  514. //status change load/saving. [Skotlex]
  515. #ifdef NEW_CARTS
  516. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  517. #else
  518. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  519. #endif
  520. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  521. if(pc_isdead(sd)){
  522. pc_setrestartvalue(sd, 0);
  523. } else {
  524. sd->status.hp = sd->battle_status.hp;
  525. sd->status.sp = sd->battle_status.sp;
  526. }
  527. sd->status.last_point.map = sd->mapindex;
  528. sd->status.last_point.x = sd->bl.x;
  529. sd->status.last_point.y = sd->bl.y;
  530. return;
  531. }
  532. if(pc_isdead(sd)) {
  533. pc_setrestartvalue(sd, 0);
  534. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  535. } else {
  536. sd->status.hp = sd->battle_status.hp;
  537. sd->status.sp = sd->battle_status.sp;
  538. sd->status.last_point.map = sd->mapindex;
  539. sd->status.last_point.x = sd->bl.x;
  540. sd->status.last_point.y = sd->bl.y;
  541. }
  542. if(map[sd->bl.m].flag.nosave) {
  543. struct map_data *m=&map[sd->bl.m];
  544. if(m->save.map)
  545. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  546. else
  547. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  548. }
  549. }
  550. /*==========================================
  551. * Off init ? Connection?
  552. *------------------------------------------*/
  553. 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) {
  554. nullpo_retv(sd);
  555. sd->bl.id = account_id;
  556. sd->status.account_id = account_id;
  557. sd->status.char_id = char_id;
  558. sd->status.sex = sex;
  559. sd->login_id1 = login_id1;
  560. sd->login_id2 = 0; // at this point, we can not know the value :(
  561. sd->client_tick = client_tick;
  562. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  563. sd->bl.type = BL_PC;
  564. sd->canlog_tick = gettick();
  565. //Required to prevent homunculus copuing a base speed of 0.
  566. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  567. }
  568. /**
  569. * Get equip point for an equip
  570. * @param sd
  571. * @param n Equip index in inventory
  572. */
  573. int pc_equippoint(struct map_session_data *sd,int n){
  574. int ep = 0;
  575. nullpo_ret(sd);
  576. if(!sd->inventory_data[n])
  577. return 0;
  578. if (!itemdb_isequip2(sd->inventory_data[n]))
  579. return 0; //Not equippable by players.
  580. ep = sd->inventory_data[n]->equip;
  581. if(sd->inventory_data[n]->look == W_DAGGER ||
  582. sd->inventory_data[n]->look == W_1HSWORD ||
  583. sd->inventory_data[n]->look == W_1HAXE) {
  584. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  585. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  586. return EQP_ARMS;
  587. }
  588. return ep;
  589. }
  590. /**
  591. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  592. * @param sd : player session
  593. * @return 0 sucess, 1:invalid sd
  594. */
  595. void pc_setinventorydata(struct map_session_data *sd)
  596. {
  597. uint8 i;
  598. nullpo_retv(sd);
  599. for(i = 0; i < MAX_INVENTORY; i++) {
  600. unsigned short id = sd->status.inventory[i].nameid;
  601. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  602. }
  603. }
  604. /**
  605. * 'Calculates' weapon type
  606. * @param sd : player
  607. */
  608. void pc_calcweapontype(struct map_session_data *sd)
  609. {
  610. nullpo_retv(sd);
  611. // single-hand
  612. if(sd->weapontype2 == W_FIST) {
  613. sd->status.weapon = sd->weapontype1;
  614. return;
  615. }
  616. if(sd->weapontype1 == W_FIST) {
  617. sd->status.weapon = sd->weapontype2;
  618. return;
  619. }
  620. // dual-wield
  621. sd->status.weapon = 0;
  622. switch (sd->weapontype1){
  623. case W_DAGGER:
  624. switch (sd->weapontype2) {
  625. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  626. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  627. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  628. }
  629. break;
  630. case W_1HSWORD:
  631. switch (sd->weapontype2) {
  632. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  633. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  634. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  635. }
  636. break;
  637. case W_1HAXE:
  638. switch (sd->weapontype2) {
  639. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  640. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  641. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  642. }
  643. }
  644. // unknown, default to right hand type
  645. if (!sd->status.weapon)
  646. sd->status.weapon = sd->weapontype1;
  647. }
  648. /**
  649. * Set equip index
  650. * @param sd : Player
  651. */
  652. void pc_setequipindex(struct map_session_data *sd)
  653. {
  654. uint16 i;
  655. nullpo_retv(sd);
  656. for (i = 0; i < EQI_MAX; i++)
  657. sd->equip_index[i] = -1;
  658. for (i = 0; i < MAX_INVENTORY; i++) {
  659. if (sd->status.inventory[i].nameid <= 0)
  660. continue;
  661. if (sd->status.inventory[i].equip) {
  662. uint8 j;
  663. for (j = 0; j < EQI_MAX; j++)
  664. if (sd->status.inventory[i].equip & equip_bitmask[j])
  665. sd->equip_index[j] = i;
  666. if (sd->status.inventory[i].equip & EQP_HAND_R) {
  667. if (sd->inventory_data[i])
  668. sd->weapontype1 = sd->inventory_data[i]->look;
  669. else
  670. sd->weapontype1 = 0;
  671. }
  672. if( sd->status.inventory[i].equip & EQP_HAND_L ) {
  673. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  674. sd->weapontype2 = sd->inventory_data[i]->look;
  675. else
  676. sd->weapontype2 = 0;
  677. }
  678. }
  679. }
  680. pc_calcweapontype(sd);
  681. }
  682. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  683. //{
  684. // int i;
  685. // struct item *item = &sd->status.inventory[eqindex];
  686. // struct item_data *data;
  687. //
  688. // //Crafted/made/hatched items.
  689. // if (itemdb_isspecial(item->card[0]))
  690. // return 1;
  691. //
  692. // /* scan for enchant armor gems */
  693. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  694. // s = MAX_SLOTS - 1;
  695. //
  696. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  697. // return( i < s ) ? 0 : 1;
  698. //}
  699. /**
  700. * Check if an item is equiped by player
  701. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  702. * @param sd : player session
  703. * @param nameid : itemid
  704. * @return 1:yes, 0:no
  705. */
  706. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  707. {
  708. uint8 i;
  709. for( i = 0; i < EQI_MAX; i++ )
  710. {
  711. short index = sd->equip_index[i], j;
  712. if( index < 0 )
  713. continue;
  714. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  715. continue;
  716. if( !sd->inventory_data[index] )
  717. continue;
  718. if( sd->inventory_data[index]->nameid == nameid )
  719. return true;
  720. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  721. if( sd->status.inventory[index].card[j] == nameid )
  722. return true;
  723. }
  724. }
  725. return false;
  726. }
  727. /**
  728. * Check adoption rules
  729. * @param p1_sd: Player 1
  730. * @param p2_sd: Player 2
  731. * @param b_sd: Player that will be adopted
  732. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  733. * ADOPT_ALREADY_ADOPTED - Already adopted
  734. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  735. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  736. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  737. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  738. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  739. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  740. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  741. */
  742. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  743. {
  744. if( !p1_sd || !p2_sd || !b_sd )
  745. return ADOPT_CHARACTER_NOT_FOUND;
  746. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  747. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  748. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  749. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  750. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  751. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  752. if( p2_sd->status.party_id != p1_sd->status.party_id )
  753. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  754. // Parents need to have their ring equipped
  755. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  756. return ADOPT_EQUIP_RINGS;
  757. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  758. return ADOPT_EQUIP_RINGS;
  759. // Already adopted a baby
  760. if( p1_sd->status.child || p2_sd->status.child ) {
  761. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  762. return ADOPT_MORE_CHILDREN;
  763. }
  764. // Parents need at least lvl 70 to adopt
  765. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  766. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  767. return ADOPT_LEVEL_70;
  768. }
  769. if( b_sd->status.partner_id ) {
  770. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  771. return ADOPT_MARRIED;
  772. }
  773. 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 ) )
  774. return ADOPT_NOT_NOVICE;
  775. return ADOPT_ALLOWED;
  776. }
  777. /*==========================================
  778. * Adoption Process
  779. *------------------------------------------*/
  780. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  781. {
  782. int job, joblevel;
  783. unsigned int jobexp;
  784. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  785. return false;
  786. // Preserve current job levels and progress
  787. joblevel = b_sd->status.job_level;
  788. jobexp = b_sd->status.job_exp;
  789. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  790. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  791. { // Success, proceed to configure parents and baby skills
  792. p1_sd->status.child = b_sd->status.char_id;
  793. p2_sd->status.child = b_sd->status.char_id;
  794. b_sd->status.father = p1_sd->status.char_id;
  795. b_sd->status.mother = p2_sd->status.char_id;
  796. // Restore progress
  797. b_sd->status.job_level = joblevel;
  798. clif_updatestatus(b_sd, SP_JOBLEVEL);
  799. b_sd->status.job_exp = jobexp;
  800. clif_updatestatus(b_sd, SP_JOBEXP);
  801. // Baby Skills
  802. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  803. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  804. // Parents Skills
  805. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  806. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  807. return true;
  808. }
  809. return false; // Job Change Fail
  810. }
  811. /*==========================================
  812. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  813. Returns:
  814. false : Cannot use/equip
  815. true : Can use/equip
  816. * Credits:
  817. [Inkfish] for first idea
  818. [Haru] for third-classes extension
  819. [Cydh] finishing :D
  820. *------------------------------------------*/
  821. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  822. while (1) {
  823. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  824. break;
  825. #ifndef RENEWAL
  826. //allow third classes to use trans. class items
  827. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  828. break;
  829. //third-baby classes can use same item too
  830. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  831. break;
  832. //don't need to decide specific rules for third-classes?
  833. //items for third classes can be used for all third classes
  834. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  835. break;
  836. #else
  837. //trans. classes (exl. third-trans.)
  838. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  839. break;
  840. //baby classes (exl. third-baby)
  841. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  842. break;
  843. //third classes (exl. third-trans. and baby-third)
  844. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  845. break;
  846. //trans-third classes
  847. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  848. break;
  849. //third-baby classes
  850. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  851. break;
  852. #endif
  853. return false;
  854. }
  855. return true;
  856. }
  857. /*=================================================
  858. * Checks if the player can equip the item at index n in inventory.
  859. * @param sd
  860. * @param n Item index in inventory
  861. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  862. *------------------------------------------------*/
  863. uint8 pc_isequip(struct map_session_data *sd,int n)
  864. {
  865. struct item_data *item;
  866. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  867. item = sd->inventory_data[n];
  868. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  869. return ITEM_EQUIP_ACK_OK;
  870. if(item == NULL)
  871. return ITEM_EQUIP_ACK_FAIL;
  872. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  873. return ITEM_EQUIP_ACK_FAILLEVEL;
  874. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  875. return ITEM_EQUIP_ACK_FAILLEVEL;
  876. if(item->sex != 2 && sd->status.sex != item->sex)
  877. return ITEM_EQUIP_ACK_FAIL;
  878. if (sd->sc.count) {
  879. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  880. return ITEM_EQUIP_ACK_FAIL;
  881. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  882. return ITEM_EQUIP_ACK_FAIL;
  883. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  884. return ITEM_EQUIP_ACK_FAIL;
  885. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  886. return ITEM_EQUIP_ACK_FAIL;
  887. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  888. return ITEM_EQUIP_ACK_FAIL;
  889. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  890. return ITEM_EQUIP_ACK_FAIL;
  891. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  892. //Spirit of Super Novice equip bonuses. [Skotlex]
  893. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  894. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  895. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  896. switch(item->look) { //In weapons, the look determines type of weapon.
  897. case W_DAGGER: //All level 4 - Daggers
  898. case W_1HSWORD: //All level 4 - 1H Swords
  899. case W_1HAXE: //All level 4 - 1H Axes
  900. case W_MACE: //All level 4 - 1H Maces
  901. case W_STAFF: //All level 4 - 1H Staves
  902. case W_2HSTAFF: //All level 4 - 2H Staves
  903. return ITEM_EQUIP_ACK_OK;
  904. }
  905. }
  906. }
  907. //fail to equip if item is restricted
  908. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  909. return ITEM_EQUIP_ACK_FAIL;
  910. //Not equipable by class. [Skotlex]
  911. if (!(1ULL<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  912. return ITEM_EQUIP_ACK_FAIL;
  913. if (!pc_isItemClass(sd,item))
  914. return ITEM_EQUIP_ACK_FAIL;
  915. return ITEM_EQUIP_ACK_OK;
  916. }
  917. /*==========================================
  918. * No problem with the session id
  919. * set the status that has been sent from char server
  920. *------------------------------------------*/
  921. 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)
  922. {
  923. int i;
  924. #ifdef BOUND_ITEMS
  925. int j;
  926. int idxlist[MAX_INVENTORY];
  927. #endif
  928. unsigned long tick = gettick();
  929. uint32 ip = session[sd->fd]->client_addr;
  930. sd->login_id2 = login_id2;
  931. sd->group_id = group_id;
  932. /* load user permissions */
  933. pc_group_pc_load(sd);
  934. memcpy(&sd->status, st, sizeof(*st));
  935. if (st->sex != sd->status.sex) {
  936. clif_authfail_fd(sd->fd, 0);
  937. return false;
  938. }
  939. //Set the map-server used job id. [Skotlex]
  940. i = pc_jobid2mapid(sd->status.class_);
  941. if (i == -1) { //Invalid class?
  942. 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);
  943. sd->status.class_ = JOB_NOVICE;
  944. sd->class_ = MAPID_NOVICE;
  945. } else
  946. sd->class_ = i;
  947. // Checks and fixes to character status data, that are required
  948. // in case of configuration change or stuff, which cannot be
  949. // checked on char-server.
  950. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  951. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  952. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  953. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  954. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  955. if(!sd->status.hp) pc_setdead(sd);
  956. sd->state.connect_new = 1;
  957. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  958. sd->invincible_timer = INVALID_TIMER;
  959. sd->npc_timer_id = INVALID_TIMER;
  960. sd->pvp_timer = INVALID_TIMER;
  961. sd->expiration_tid = INVALID_TIMER;
  962. sd->autotrade_tid = INVALID_TIMER;
  963. #ifdef SECURE_NPCTIMEOUT
  964. // Initialize to defaults/expected
  965. sd->npc_idle_timer = INVALID_TIMER;
  966. sd->npc_idle_tick = tick;
  967. sd->npc_idle_type = NPCT_INPUT;
  968. sd->state.ignoretimeout = false;
  969. #endif
  970. sd->canuseitem_tick = tick;
  971. sd->canusecashfood_tick = tick;
  972. sd->canequip_tick = tick;
  973. sd->cantalk_tick = tick;
  974. sd->canskill_tick = tick;
  975. sd->cansendmail_tick = tick;
  976. sd->idletime = last_tick;
  977. for(i = 0; i < MAX_SPIRITBALL; i++)
  978. sd->spirit_timer[i] = INVALID_TIMER;
  979. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  980. sd->autobonus[i].active = INVALID_TIMER;
  981. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  982. sd->autobonus2[i].active = INVALID_TIMER;
  983. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  984. sd->autobonus3[i].active = INVALID_TIMER;
  985. if (battle_config.item_auto_get)
  986. sd->state.autoloot = 10000;
  987. if (battle_config.disp_experience)
  988. sd->state.showexp = 1;
  989. if (battle_config.disp_zeny)
  990. sd->state.showzeny = 1;
  991. #ifdef VIP_ENABLE
  992. if (!battle_config.vip_disp_rate)
  993. sd->vip.disableshowrate = 1;
  994. #endif
  995. if (!(battle_config.display_skill_fail&2))
  996. sd->state.showdelay = 1;
  997. pc_setinventorydata(sd);
  998. pc_setequipindex(sd);
  999. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1000. sd->status.option &= ~OPTION_INVISIBLE;
  1001. }
  1002. status_change_init(&sd->bl);
  1003. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1004. //Set here because we need the inventory data for weapon sprite parsing.
  1005. status_set_viewdata(&sd->bl, sd->status.class_);
  1006. unit_dataset(&sd->bl);
  1007. sd->guild_x = -1;
  1008. sd->guild_y = -1;
  1009. sd->delayed_damage = 0;
  1010. // Event Timers
  1011. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1012. sd->eventtimer[i] = INVALID_TIMER;
  1013. // Rental Timer
  1014. sd->rental_timer = INVALID_TIMER;
  1015. for( i = 0; i < 3; i++ )
  1016. sd->hate_mob[i] = -1;
  1017. sd->quest_log = NULL;
  1018. sd->num_quests = 0;
  1019. sd->avail_quests = 0;
  1020. sd->save_quest = false;
  1021. sd->count_rewarp = 0;
  1022. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1023. sd->regs.arrays = NULL;
  1024. sd->vars_dirty = false;
  1025. sd->vars_ok = false;
  1026. sd->vars_received = 0x0;
  1027. sd->qi_display = NULL;
  1028. sd->qi_count = 0;
  1029. //warp player
  1030. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1031. 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);
  1032. // try warping to a default map instead (church graveyard)
  1033. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1034. // if we fail again
  1035. clif_authfail_fd(sd->fd, 0);
  1036. return false;
  1037. }
  1038. }
  1039. clif_authok(sd);
  1040. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1041. sd->die_counter=-1;
  1042. //display login notice
  1043. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1044. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1045. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1046. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1047. sd->status.name, sd->status.account_id, sd->status.char_id,
  1048. sd->packet_ver, CONVIP(ip), sd->group_id);
  1049. // Send friends list
  1050. clif_friendslist_send(sd);
  1051. if( !changing_mapservers ) {
  1052. if (battle_config.display_version == 1)
  1053. pc_show_version(sd);
  1054. // Message of the Day [Valaris]
  1055. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1056. if (battle_config.motd_type)
  1057. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  1058. else
  1059. clif_displaymessage(sd->fd, motd_text[i]);
  1060. }
  1061. if (expiration_time != 0)
  1062. sd->expiration_time = expiration_time;
  1063. /**
  1064. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1065. **/
  1066. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1067. }
  1068. /**
  1069. * Check if player have any item cooldowns on
  1070. **/
  1071. pc_itemcd_do(sd,true);
  1072. pc_validate_skill(sd);
  1073. #ifdef BOUND_ITEMS
  1074. // Party bound item check
  1075. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,BOUND_PARTY,idxlist))) { // Party was deleted while character offline
  1076. for(i=0;i<j;i++)
  1077. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  1078. }
  1079. #endif
  1080. /* [Ind] */
  1081. sd->sc_display = NULL;
  1082. sd->sc_display_count = 0;
  1083. // Player has not yet received the CashShop list
  1084. sd->status.cashshop_sent = false;
  1085. sd->last_addeditem_index = -1;
  1086. sd->bonus_script.head = NULL;
  1087. sd->bonus_script.count = 0;
  1088. #if PACKETVER >= 20150513
  1089. sd->hatEffectIDs = NULL;
  1090. sd->hatEffectCount = 0;
  1091. #endif
  1092. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1093. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1094. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1095. clif_updatestatus(sd, SP_BASEEXP);
  1096. }
  1097. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1098. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1099. clif_updatestatus(sd, SP_JOBEXP);
  1100. }
  1101. // Request all registries (auth is considered completed whence they arrive)
  1102. intif_request_registry(sd,7);
  1103. return true;
  1104. }
  1105. /*==========================================
  1106. * Closes a connection because it failed to be authenticated from the char server.
  1107. *------------------------------------------*/
  1108. void pc_authfail(struct map_session_data *sd)
  1109. {
  1110. clif_authfail_fd(sd->fd, 0);
  1111. return;
  1112. }
  1113. /**
  1114. * Player register a bl as hatred
  1115. * @param sd : player session
  1116. * @param pos : hate position [0;2]
  1117. * @param bl : target bl
  1118. * @return false:failed, true:success
  1119. */
  1120. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1121. {
  1122. int class_;
  1123. if (!sd || !bl || pos < 0 || pos > 2)
  1124. return false;
  1125. if (sd->hate_mob[pos] != -1)
  1126. { //Can't change hate targets.
  1127. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1128. return false;
  1129. }
  1130. class_ = status_get_class(bl);
  1131. if (!pcdb_checkid(class_)) {
  1132. unsigned int max_hp = status_get_max_hp(bl);
  1133. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1134. return false;
  1135. if (pos != status_get_size(bl))
  1136. return false; //Wrong size
  1137. }
  1138. sd->hate_mob[pos] = class_;
  1139. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1140. clif_hate_info(sd, pos, class_, 1);
  1141. return true;
  1142. }
  1143. /*==========================================
  1144. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1145. *------------------------------------------*/
  1146. void pc_reg_received(struct map_session_data *sd)
  1147. {
  1148. uint8 i;
  1149. sd->vars_ok = true;
  1150. sd->change_level_2nd = pc_readglobalreg(sd, add_str("jobchange_level"));
  1151. sd->change_level_3rd = pc_readglobalreg(sd, add_str("jobchange_level_3rd"));
  1152. sd->die_counter = pc_readglobalreg(sd, add_str("PC_DIE_COUNTER"));
  1153. sd->langtype = pc_readaccountreg(sd, add_str("#langtype"));
  1154. if (msg_checklangtype(sd->langtype,true) < 0)
  1155. sd->langtype = 0; //invalid langtype reset to default
  1156. // Cash shop
  1157. sd->cashPoints = pc_readaccountreg(sd, add_str("#CASHPOINTS"));
  1158. sd->kafraPoints = pc_readaccountreg(sd, add_str("#KAFRAPOINTS"));
  1159. // Cooking Exp
  1160. sd->cook_mastery = pc_readglobalreg(sd, add_str("COOK_MASTERY"));
  1161. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1162. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1163. sd->mission_mobid = pc_readglobalreg(sd, add_str("TK_MISSION_ID"));
  1164. sd->mission_count = pc_readglobalreg(sd, add_str("TK_MISSION_COUNT"));
  1165. }
  1166. if (battle_config.feature_banking)
  1167. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1168. if (battle_config.feature_roulette) {
  1169. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1170. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1171. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1172. }
  1173. sd->roulette.prizeIdx = -1;
  1174. //SG map and mob read [Komurka]
  1175. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1176. uint16 j;
  1177. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1178. sd->feel_map[i].index = j;
  1179. sd->feel_map[i].m = map_mapindex2mapid(j);
  1180. } else {
  1181. sd->feel_map[i].index = 0;
  1182. sd->feel_map[i].m = -1;
  1183. }
  1184. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1185. }
  1186. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1187. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1188. sd->cloneskill_idx = skill_get_index(skid);
  1189. if (sd->cloneskill_idx > 0) {
  1190. sd->status.skill[sd->cloneskill_idx].id = skid;
  1191. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1192. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1193. sd->status.skill[sd->cloneskill_idx].lv = i;
  1194. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1195. }
  1196. }
  1197. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1198. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1199. sd->reproduceskill_idx = skill_get_index(skid);
  1200. if (sd->reproduceskill_idx > 0) {
  1201. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1202. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1203. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1204. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1205. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1206. }
  1207. }
  1208. //Weird... maybe registries were reloaded?
  1209. if (sd->state.active)
  1210. return;
  1211. sd->state.active = 1;
  1212. if (sd->status.party_id)
  1213. party_member_joined(sd);
  1214. if (sd->status.guild_id)
  1215. guild_member_joined(sd);
  1216. // pet
  1217. if (sd->status.pet_id > 0)
  1218. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1219. // Homunculus [albator]
  1220. if( sd->status.hom_id > 0 )
  1221. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1222. if( sd->status.mer_id > 0 )
  1223. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1224. if( sd->status.ele_id > 0 )
  1225. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1226. map_addiddb(&sd->bl);
  1227. map_delnickdb(sd->status.char_id, sd->status.name);
  1228. if (!chrif_auth_finished(sd))
  1229. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1230. pc_load_combo(sd);
  1231. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  1232. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1233. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1234. chrif_bsdata_request(sd->status.char_id);
  1235. sd->storage_size = MIN_STORAGE; //default to min
  1236. #ifdef VIP_ENABLE
  1237. sd->vip.time = 0;
  1238. sd->vip.enabled = 0;
  1239. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1240. #endif
  1241. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1242. intif_request_questlog(sd);
  1243. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1244. sd->state.connect_new = 1;
  1245. clif_parse_LoadEndAck(sd->fd, sd);
  1246. }
  1247. if( sd->sc.option&OPTION_INVISIBLE ) {
  1248. sd->vd.class_ = INVISIBLE_CLASS;
  1249. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1250. // decrement the number of pvp players on the map
  1251. map[sd->bl.m].users_pvp--;
  1252. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1253. // unregister the player for ranking
  1254. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1255. sd->pvp_timer = INVALID_TIMER;
  1256. }
  1257. clif_changeoption( &sd->bl );
  1258. }
  1259. pc_check_expiration(sd);
  1260. if( sd->state.autotrade ) {
  1261. clif_parse_LoadEndAck(sd->fd, sd);
  1262. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  1263. }
  1264. }
  1265. static int pc_calc_skillpoint(struct map_session_data* sd)
  1266. {
  1267. uint16 i, skill_point = 0;
  1268. nullpo_ret(sd);
  1269. for(i = 1; i < MAX_SKILL; i++) {
  1270. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1271. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1272. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1273. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1274. )
  1275. {
  1276. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1277. skill_point += sd->status.skill[i].lv;
  1278. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1279. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1280. }
  1281. }
  1282. }
  1283. return skill_point;
  1284. }
  1285. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1286. uint16 i = 0;
  1287. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1288. return false;
  1289. /**
  1290. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1291. * skill tree and since they have no icons they'll give resource errors
  1292. * Get ALL skills except npc/guild ones. [Skotlex]
  1293. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1294. **/
  1295. for( i = 0; i < MAX_SKILL; i++ ) {
  1296. uint16 skill_id = skill_idx2id(i);
  1297. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1298. continue;
  1299. switch (skill_id) {
  1300. case SM_SELFPROVOKE:
  1301. case AB_DUPLELIGHT_MELEE:
  1302. case AB_DUPLELIGHT_MAGIC:
  1303. case WL_CHAINLIGHTNING_ATK:
  1304. case WL_TETRAVORTEX_FIRE:
  1305. case WL_TETRAVORTEX_WATER:
  1306. case WL_TETRAVORTEX_WIND:
  1307. case WL_TETRAVORTEX_GROUND:
  1308. case WL_SUMMON_ATK_FIRE:
  1309. case WL_SUMMON_ATK_WIND:
  1310. case WL_SUMMON_ATK_WATER:
  1311. case WL_SUMMON_ATK_GROUND:
  1312. case LG_OVERBRAND_BRANDISH:
  1313. case LG_OVERBRAND_PLUSATK:
  1314. case WM_SEVERE_RAINSTORM_MELEE:
  1315. case RL_R_TRIP_PLUSATK:
  1316. case SG_DEVIL:
  1317. case MO_TRIPLEATTACK:
  1318. case RG_SNATCHER:
  1319. continue;
  1320. default:
  1321. {
  1322. uint8 lv = (uint8)skill_get_max(skill_id);
  1323. if (lv > 0) {
  1324. sd->status.skill[i].id = skill_id;
  1325. if (addlv)
  1326. sd->status.skill[i].lv = lv;
  1327. }
  1328. }
  1329. break;
  1330. }
  1331. }
  1332. return true;
  1333. }
  1334. /*==========================================
  1335. * Calculation of skill level.
  1336. * @param sd
  1337. *------------------------------------------*/
  1338. void pc_calc_skilltree(struct map_session_data *sd)
  1339. {
  1340. int i, flag;
  1341. int c = 0;
  1342. nullpo_retv(sd);
  1343. i = pc_calc_skilltree_normalize_job(sd);
  1344. c = pc_mapid2jobid(i, sd->status.sex);
  1345. if( c == -1 )
  1346. { //Unable to normalize job??
  1347. 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);
  1348. return;
  1349. }
  1350. c = pc_class2idx(c);
  1351. for( i = 0; i < MAX_SKILL; i++ ) {
  1352. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1353. sd->status.skill[i].id = 0; //First clear skills.
  1354. /* permanent skills that must be re-checked */
  1355. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1356. uint16 sk_id = skill_idx2id(i);
  1357. if (!sk_id) {
  1358. sd->status.skill[i].id = 0;
  1359. sd->status.skill[i].lv = 0;
  1360. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1361. continue;
  1362. }
  1363. switch (sk_id) {
  1364. case NV_TRICKDEAD:
  1365. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1366. sd->status.skill[i].id = 0;
  1367. sd->status.skill[i].lv = 0;
  1368. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1369. }
  1370. break;
  1371. }
  1372. }
  1373. }
  1374. for( i = 0; i < MAX_SKILL; i++ ) {
  1375. uint16 skill_id = 0;
  1376. // Restore original level of skills after deleting earned skills.
  1377. 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 ) {
  1378. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1379. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1380. }
  1381. //Enable Bard/Dancer spirit linked skills.
  1382. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1383. continue;
  1384. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1385. //Link Dancer skills to bard.
  1386. if( sd->status.sex ) {
  1387. if( sd->status.skill[i-8].lv < 10 )
  1388. continue;
  1389. sd->status.skill[i].id = skill_id;
  1390. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1391. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1392. }
  1393. //Link Bard skills to dancer.
  1394. else {
  1395. if( sd->status.skill[i].lv < 10 )
  1396. continue;
  1397. sd->status.skill[i-8].id = skill_id - 8;
  1398. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1399. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1400. }
  1401. }
  1402. }
  1403. // Removes Taekwon Ranker skill bonus
  1404. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1405. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1406. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1407. uint16 sk_id = skill_tree[c_][i].skill_id;
  1408. uint16 sk_idx = 0;
  1409. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1410. continue;
  1411. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1412. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1413. continue;
  1414. sd->status.skill[sk_idx].id = 0;
  1415. }
  1416. }
  1417. }
  1418. // Grant all skills
  1419. pc_grant_allskills(sd, false);
  1420. do {
  1421. uint16 skid = 0;
  1422. flag = 0;
  1423. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1424. bool fail = false;
  1425. uint16 sk_idx = skill_get_index(skid);
  1426. if (sd->status.skill[sk_idx].id)
  1427. continue; //Skill already known.
  1428. if (!battle_config.skillfree) {
  1429. uint8 j;
  1430. // Checking required skills
  1431. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1432. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1433. uint16 sk_need_idx = 0;
  1434. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1435. short sk_need = sk_need_id;
  1436. 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)
  1437. sk_need = 0; //Not learned.
  1438. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1439. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1440. else
  1441. sk_need = pc_checkskill(sd,sk_need_id);
  1442. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1443. fail = true;
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1449. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1450. class_ = pc_class2idx(class_);
  1451. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1452. fail = true; // base level requirement wasn't satisfied
  1453. }
  1454. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1455. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1456. class_ = pc_class2idx(class_);
  1457. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1458. fail = true; // job level requirement wasn't satisfied
  1459. }
  1460. }
  1461. if (!fail) {
  1462. int inf2 = skill_get_inf2(skid);
  1463. if (!sd->status.skill[sk_idx].lv && (
  1464. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1465. inf2&INF2_WEDDING_SKILL ||
  1466. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1467. ))
  1468. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1469. sd->status.skill[sk_idx].id = skid;
  1470. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1471. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1472. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1473. }
  1474. flag = 1; // skill list has changed, perform another pass
  1475. }
  1476. }
  1477. } while(flag);
  1478. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1479. unsigned short skid = 0;
  1480. /* Taekwon Ranker Bonus Skill Tree
  1481. ============================================
  1482. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1483. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1484. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1485. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1486. uint16 sk_idx = 0;
  1487. if (!(sk_idx = skill_get_index(skid)))
  1488. continue;
  1489. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1490. continue; //Do not include Quest/Wedding skills.
  1491. if( sd->status.skill[sk_idx].id == 0 ) {
  1492. sd->status.skill[sk_idx].id = skid;
  1493. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1494. } else if( skid != NV_BASIC )
  1495. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1496. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1497. }
  1498. }
  1499. }
  1500. //Checks if you can learn a new skill after having leveled up a skill.
  1501. static void pc_check_skilltree(struct map_session_data *sd)
  1502. {
  1503. int i, flag = 0;
  1504. int c = 0;
  1505. if (battle_config.skillfree)
  1506. return; //Function serves no purpose if this is set
  1507. i = pc_calc_skilltree_normalize_job(sd);
  1508. c = pc_mapid2jobid(i, sd->status.sex);
  1509. if (c == -1) { //Unable to normalize job??
  1510. 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);
  1511. return;
  1512. }
  1513. c = pc_class2idx(c);
  1514. do {
  1515. uint16 skid = 0;
  1516. flag = 0;
  1517. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1518. uint16 sk_idx = skill_get_index(skid);
  1519. bool fail = false;
  1520. uint8 j = 0;
  1521. if (sd->status.skill[sk_idx].id) //Already learned
  1522. continue;
  1523. // Checking required skills
  1524. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1525. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1526. uint16 sk_need_idx = 0;
  1527. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1528. short sk_need = sk_need_id;
  1529. 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)
  1530. sk_need = 0; //Not learned.
  1531. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1532. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1533. else
  1534. sk_need = pc_checkskill(sd,sk_need_id);
  1535. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1536. fail = true;
  1537. break;
  1538. }
  1539. }
  1540. }
  1541. if( fail )
  1542. continue;
  1543. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1544. continue;
  1545. j = skill_get_inf2(skid);
  1546. if( !sd->status.skill[sk_idx].lv && (
  1547. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1548. j&INF2_WEDDING_SKILL ||
  1549. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1550. ) )
  1551. continue; //Cannot be learned via normal means.
  1552. sd->status.skill[sk_idx].id = skid;
  1553. flag = 1;
  1554. }
  1555. } while(flag);
  1556. }
  1557. // Make sure all the skills are in the correct condition
  1558. // before persisting to the backend.. [MouseJstr]
  1559. void pc_clean_skilltree(struct map_session_data *sd)
  1560. {
  1561. uint16 i;
  1562. for (i = 0; i < MAX_SKILL; i++){
  1563. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1564. sd->status.skill[i].id = 0;
  1565. sd->status.skill[i].lv = 0;
  1566. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1567. }
  1568. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1569. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1570. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1571. }
  1572. }
  1573. }
  1574. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1575. {
  1576. int skill_point, novice_skills;
  1577. int c = sd->class_;
  1578. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1579. return c;
  1580. skill_point = pc_calc_skillpoint(sd);
  1581. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1582. // limit 1st class and above to novice job levels
  1583. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1584. {
  1585. c = MAPID_NOVICE;
  1586. }
  1587. // limit 2nd class and above to first class job levels (super novices are exempt)
  1588. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1589. {
  1590. // regenerate change_level_2nd
  1591. if (!sd->change_level_2nd)
  1592. {
  1593. if (sd->class_&JOBL_THIRD)
  1594. {
  1595. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1596. if (!sd->change_level_3rd)
  1597. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1598. else
  1599. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1600. - (sd->status.job_level - 1)
  1601. - (sd->change_level_3rd - 1)
  1602. - novice_skills;
  1603. }
  1604. else
  1605. {
  1606. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1607. - (sd->status.job_level - 1)
  1608. - novice_skills;
  1609. }
  1610. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  1611. }
  1612. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1613. {
  1614. c &= MAPID_BASEMASK;
  1615. }
  1616. // limit 3rd class to 2nd class/trans job levels
  1617. else if(sd->class_&JOBL_THIRD)
  1618. {
  1619. // regenerate change_level_3rd
  1620. if (!sd->change_level_3rd)
  1621. {
  1622. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1623. - (sd->status.job_level - 1)
  1624. - (sd->change_level_2nd - 1)
  1625. - novice_skills;
  1626. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  1627. }
  1628. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1629. c &= MAPID_UPPERMASK;
  1630. }
  1631. }
  1632. // restore non-limiting flags
  1633. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1634. return c;
  1635. }
  1636. /*==========================================
  1637. * Updates the weight status
  1638. *------------------------------------------
  1639. * 1: overweight 50%
  1640. * 2: overweight 90%
  1641. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1642. */
  1643. void pc_updateweightstatus(struct map_session_data *sd)
  1644. {
  1645. int old_overweight;
  1646. int new_overweight;
  1647. nullpo_retv(sd);
  1648. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1649. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1650. if( old_overweight == new_overweight )
  1651. return; // no change
  1652. // stop old status change
  1653. if( old_overweight == 1 )
  1654. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1655. else if( old_overweight == 2 )
  1656. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1657. // start new status change
  1658. if( new_overweight == 1 )
  1659. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1660. else if( new_overweight == 2 )
  1661. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1662. // update overweight status
  1663. sd->regen.state.overweight = new_overweight;
  1664. }
  1665. int pc_disguise(struct map_session_data *sd, int class_)
  1666. {
  1667. if (!class_ && !sd->disguise)
  1668. return 0;
  1669. if (class_ && sd->disguise == class_)
  1670. return 0;
  1671. if(sd->sc.option&OPTION_INVISIBLE)
  1672. { //Character is invisible. Stealth class-change. [Skotlex]
  1673. sd->disguise = class_; //viewdata is set on uncloaking.
  1674. return 2;
  1675. }
  1676. if (sd->bl.prev != NULL) {
  1677. pc_stop_walking(sd, 0);
  1678. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1679. }
  1680. if (!class_) {
  1681. sd->disguise = 0;
  1682. class_ = sd->status.class_;
  1683. } else
  1684. sd->disguise=class_;
  1685. status_set_viewdata(&sd->bl, class_);
  1686. clif_changeoption(&sd->bl);
  1687. if (sd->bl.prev != NULL) {
  1688. clif_spawn(&sd->bl);
  1689. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1690. { //It seems the cart info is lost on undisguise.
  1691. clif_cartlist(sd);
  1692. clif_updatestatus(sd,SP_CARTINFO);
  1693. }
  1694. if (sd->chatID) {
  1695. struct chat_data* cd;
  1696. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1697. clif_dispchat(cd,0);
  1698. }
  1699. }
  1700. return 1;
  1701. }
  1702. /// Show error message
  1703. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1704. /// Check for valid Element, break & show error message if invalid Element
  1705. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1706. /// Check for valid Race, break & show error message if invalid Race
  1707. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1708. /// Check for valid Race2, break & show error message if invalid Race2
  1709. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1710. /// Check for valid Class, break & show error message if invalid Class
  1711. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1712. /// Check for valid Size, break & show error message if invalid Size
  1713. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1714. /// Check for valid SC, break & show error message if invalid SC
  1715. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1716. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1717. {
  1718. uint8 i;
  1719. if( !rate )
  1720. return;
  1721. for( i = 0; i < max && spell[i].id; i++ )
  1722. {
  1723. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1724. {
  1725. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1726. return;
  1727. rate += spell[i].rate;
  1728. break;
  1729. }
  1730. }
  1731. if (i == max) {
  1732. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1733. return;
  1734. }
  1735. spell[i].id = id;
  1736. spell[i].lv = lv;
  1737. spell[i].rate = rate;
  1738. //Auto-update flag value.
  1739. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1740. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1741. if (!(flag&BF_SKILLMASK)) {
  1742. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1743. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1744. }
  1745. spell[i].flag|= flag;
  1746. spell[i].card_id = card_id;
  1747. }
  1748. 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)
  1749. {
  1750. uint8 i;
  1751. if( !rate )
  1752. return;
  1753. for( i = 0; i < max && spell[i].id; i++ )
  1754. {
  1755. ; // each autospell works independently
  1756. }
  1757. if( i == max )
  1758. {
  1759. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1760. return;
  1761. }
  1762. spell[i].flag = src_skill;
  1763. spell[i].id = id;
  1764. spell[i].lv = lv;
  1765. spell[i].rate = rate;
  1766. spell[i].card_id = card_id;
  1767. return;
  1768. }
  1769. /**
  1770. * Add inflict effect bonus for player while attacking/atatcked
  1771. * @param effect Effect array
  1772. * @param pmax Max array
  1773. * @param sc SC/Effect type
  1774. * @param rate Success chance
  1775. * @param arrow_rate success chance if bonus comes from arrow-type item
  1776. * @param flag Target flag
  1777. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1778. **/
  1779. 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)
  1780. {
  1781. uint16 i;
  1782. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1783. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1784. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1785. flag |= ATF_TARGET; //Default target: enemy.
  1786. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1787. flag |= ATF_WEAPON; //Default type: weapon.
  1788. if (!duration)
  1789. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1790. for (i = 0; i < pmax && effect[i].flag; i++) {
  1791. if (effect[i].sc == sc && effect[i].flag == flag) {
  1792. effect[i].rate += rate;
  1793. effect[i].arrow_rate += arrow_rate;
  1794. effect[i].duration = umax(effect[i].duration, duration);
  1795. return;
  1796. }
  1797. }
  1798. if (i == pmax) {
  1799. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1800. return;
  1801. }
  1802. effect[i].sc = sc;
  1803. effect[i].rate = rate;
  1804. effect[i].arrow_rate = arrow_rate;
  1805. effect[i].flag = flag;
  1806. effect[i].duration = duration;
  1807. }
  1808. /**
  1809. * Add inflict effect bonus for player while attacking using skill
  1810. * @param effect Effect array
  1811. * @param pmax Max array
  1812. * @param sc SC/Effect type
  1813. * @param rate Success chance
  1814. * @param flag Target flag
  1815. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1816. **/
  1817. 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)
  1818. {
  1819. uint8 i;
  1820. if (!duration)
  1821. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1822. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1823. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1824. effect[i].rate += rate;
  1825. effect[i].duration = umax(effect[i].duration, duration);
  1826. return;
  1827. }
  1828. }
  1829. if( i == pmax ) {
  1830. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1831. return;
  1832. }
  1833. effect[i].sc = sc;
  1834. effect[i].rate = rate;
  1835. effect[i].skill_id = skill_id;
  1836. effect[i].target = target;
  1837. effect[i].duration = duration;
  1838. }
  1839. /** Adjust/add drop rate modifier for player
  1840. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1841. * @param max: Max bonus can be received
  1842. * @param nameid: item id that will be dropped
  1843. * @param group: group id
  1844. * @param class_: target class
  1845. * @param race: target race. if < 0, means monster_id
  1846. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1847. */
  1848. 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)
  1849. {
  1850. uint8 i;
  1851. if (!nameid && !group) {
  1852. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1853. return;
  1854. }
  1855. if (nameid && !itemdb_exists(nameid)) {
  1856. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1857. return;
  1858. }
  1859. if (group && !itemdb_group_exists(group)) {
  1860. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1861. return;
  1862. }
  1863. //Apply config rate adjustment settings.
  1864. if (rate >= 0) { //Absolute drop.
  1865. if (battle_config.item_rate_adddrop != 100)
  1866. rate = rate*battle_config.item_rate_adddrop/100;
  1867. if (rate < battle_config.item_drop_adddrop_min)
  1868. rate = battle_config.item_drop_adddrop_min;
  1869. else if (rate > battle_config.item_drop_adddrop_max)
  1870. rate = battle_config.item_drop_adddrop_max;
  1871. } else { //Relative drop, max/min limits are applied at drop time.
  1872. if (battle_config.item_rate_adddrop != 100)
  1873. rate = rate*battle_config.item_rate_adddrop/100;
  1874. if (rate > -1)
  1875. rate = -1;
  1876. }
  1877. //Find match entry, and adjust the rate only
  1878. for (i = 0; i < max; i++) {
  1879. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1880. continue;
  1881. if (drop[i].nameid == nameid &&
  1882. drop[i].group == group &&
  1883. drop[i].race == race &&
  1884. drop[i].class_ == class_
  1885. )
  1886. {
  1887. //Adjust the rate if it has same classification
  1888. if ((rate < 0 && drop[i].rate < 0) ||
  1889. (rate > 0 && drop[i].rate > 0))
  1890. {
  1891. drop[i].rate += rate;
  1892. return;
  1893. }
  1894. }
  1895. }
  1896. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1897. if (i >= max) {
  1898. 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);
  1899. return;
  1900. }
  1901. drop[i].nameid = nameid;
  1902. drop[i].group = group;
  1903. drop[i].race = race;
  1904. drop[i].class_ = class_;
  1905. drop[i].rate = rate;
  1906. }
  1907. 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)
  1908. {
  1909. int i;
  1910. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1911. if( i == max )
  1912. {
  1913. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1914. return false;
  1915. }
  1916. if( !onskill )
  1917. {
  1918. if( !(flag&BF_RANGEMASK) )
  1919. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1920. if( !(flag&BF_WEAPONMASK) )
  1921. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1922. if( !(flag&BF_SKILLMASK) )
  1923. {
  1924. if( flag&(BF_MAGIC|BF_MISC) )
  1925. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1926. if( flag&BF_WEAPON )
  1927. flag|=BF_NORMAL|BF_SKILL;
  1928. }
  1929. }
  1930. bonus[i].rate = rate;
  1931. bonus[i].duration = dur;
  1932. bonus[i].active = INVALID_TIMER;
  1933. bonus[i].atk_type = flag;
  1934. bonus[i].pos = pos;
  1935. bonus[i].bonus_script = aStrdup(script);
  1936. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1937. return true;
  1938. }
  1939. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1940. {
  1941. int i;
  1942. if (!sd)
  1943. return;
  1944. for( i = 0; i < max; i++ )
  1945. {
  1946. if( autobonus[i].active != INVALID_TIMER )
  1947. {
  1948. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1949. {
  1950. if( autobonus[i].bonus_script )
  1951. {
  1952. int j;
  1953. unsigned int equip_pos_idx = 0;
  1954. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1955. for(j = 0; j < EQI_MAX; j++) {
  1956. if(sd->equip_index[j] >= 0)
  1957. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1958. }
  1959. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  1960. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  1961. }
  1962. continue;
  1963. }
  1964. else
  1965. { // Logout / Unequipped an item with an activated bonus
  1966. delete_timer(autobonus[i].active,pc_endautobonus);
  1967. autobonus[i].active = INVALID_TIMER;
  1968. }
  1969. }
  1970. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1971. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1972. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1973. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1974. autobonus[i].active = INVALID_TIMER;
  1975. }
  1976. }
  1977. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1978. {
  1979. if (!sd || !autobonus)
  1980. return;
  1981. if( autobonus->other_script )
  1982. {
  1983. int j;
  1984. unsigned int equip_pos_idx = 0;
  1985. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1986. for(j = 0; j < EQI_MAX; j++) {
  1987. if(sd->equip_index[j] >= 0)
  1988. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1989. }
  1990. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  1991. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  1992. }
  1993. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1994. sd->state.autobonus |= autobonus->pos;
  1995. status_calc_pc(sd,SCO_FORCE);
  1996. }
  1997. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1998. {
  1999. struct map_session_data *sd = map_id2sd(id);
  2000. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2001. nullpo_ret(sd);
  2002. nullpo_ret(autobonus);
  2003. autobonus->active = INVALID_TIMER;
  2004. sd->state.autobonus &= ~autobonus->pos;
  2005. status_calc_pc(sd,SCO_FORCE);
  2006. return 0;
  2007. }
  2008. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2009. {
  2010. uint8 i;
  2011. struct weapon_data* wd;
  2012. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2013. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2014. if (i == MAX_PC_BONUS)
  2015. {
  2016. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2017. return;
  2018. }
  2019. if (!(flag&BF_RANGEMASK))
  2020. flag |= BF_SHORT|BF_LONG;
  2021. if (!(flag&BF_WEAPONMASK))
  2022. flag |= BF_WEAPON;
  2023. if (!(flag&BF_SKILLMASK))
  2024. {
  2025. if (flag&(BF_MAGIC|BF_MISC))
  2026. flag |= BF_SKILL;
  2027. if (flag&BF_WEAPON)
  2028. flag |= BF_NORMAL|BF_SKILL;
  2029. }
  2030. wd->addele2[i].ele = ele;
  2031. wd->addele2[i].rate = rate;
  2032. wd->addele2[i].flag = flag;
  2033. }
  2034. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2035. {
  2036. uint8 i;
  2037. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2038. if (i == MAX_PC_BONUS)
  2039. {
  2040. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2041. return;
  2042. }
  2043. if (!(flag&BF_RANGEMASK))
  2044. flag |= BF_SHORT|BF_LONG;
  2045. if (!(flag&BF_WEAPONMASK))
  2046. flag |= BF_WEAPON;
  2047. if (!(flag&BF_SKILLMASK))
  2048. {
  2049. if (flag&(BF_MAGIC|BF_MISC))
  2050. flag |= BF_SKILL;
  2051. if (flag&BF_WEAPON)
  2052. flag |= BF_NORMAL|BF_SKILL;
  2053. }
  2054. sd->subele2[i].ele = ele;
  2055. sd->subele2[i].rate = rate;
  2056. sd->subele2[i].flag = flag;
  2057. }
  2058. /** Add item group heal rate bonus to player
  2059. * @param sd Player
  2060. * @param group_id Item Group ID
  2061. * @param rate
  2062. * @author Cydh
  2063. */
  2064. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2065. struct s_pc_itemgrouphealrate *entry;
  2066. uint8 i;
  2067. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2068. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2069. break;
  2070. }
  2071. if (i != sd->itemgrouphealrate_count) {
  2072. sd->itemgrouphealrate[i]->rate += rate;
  2073. return;
  2074. }
  2075. if (i >= UINT8_MAX) {
  2076. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2077. return;
  2078. }
  2079. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2080. entry->group_id = group_id;
  2081. entry->rate = rate;
  2082. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2083. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2084. }
  2085. /** Clear item group heal rate from player
  2086. * @param sd Player
  2087. * @author Cydh
  2088. */
  2089. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2090. if (!sd || !sd->itemgrouphealrate_count)
  2091. return;
  2092. else {
  2093. uint8 i;
  2094. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2095. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2096. sd->itemgrouphealrate_count = 0;
  2097. aFree(sd->itemgrouphealrate);
  2098. sd->itemgrouphealrate = NULL;
  2099. }
  2100. }
  2101. /*==========================================
  2102. * Add a bonus(type) to player sd
  2103. * format: bonus bBonusName,val;
  2104. * @param sd
  2105. * @param type Bonus type used by bBonusName
  2106. * @param val Value that usually for rate or fixed value
  2107. *------------------------------------------*/
  2108. void pc_bonus(struct map_session_data *sd,int type,int val)
  2109. {
  2110. struct status_data *status;
  2111. int bonus;
  2112. nullpo_retv(sd);
  2113. status = &sd->base_status;
  2114. switch(type){
  2115. case SP_STR:
  2116. case SP_AGI:
  2117. case SP_VIT:
  2118. case SP_INT:
  2119. case SP_DEX:
  2120. case SP_LUK:
  2121. if(sd->state.lr_flag != 2)
  2122. sd->param_bonus[type-SP_STR]+=val;
  2123. break;
  2124. case SP_ATK1:
  2125. if(!sd->state.lr_flag) {
  2126. bonus = status->rhw.atk + val;
  2127. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2128. }
  2129. else if(sd->state.lr_flag == 1) {
  2130. bonus = status->lhw.atk + val;
  2131. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2132. }
  2133. break;
  2134. case SP_ATK2:
  2135. if(!sd->state.lr_flag) {
  2136. bonus = status->rhw.atk2 + val;
  2137. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2138. }
  2139. else if(sd->state.lr_flag == 1) {
  2140. bonus = status->lhw.atk2 + val;
  2141. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2142. }
  2143. break;
  2144. case SP_BASE_ATK:
  2145. if(sd->state.lr_flag != 2) {
  2146. #ifdef RENEWAL
  2147. sd->bonus.eatk += val;
  2148. #else
  2149. bonus = status->batk + val;
  2150. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2151. #endif
  2152. }
  2153. break;
  2154. case SP_DEF1:
  2155. if(sd->state.lr_flag != 2) {
  2156. bonus = status->def + val;
  2157. #ifdef RENEWAL
  2158. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2159. #else
  2160. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2161. #endif
  2162. }
  2163. break;
  2164. case SP_DEF2:
  2165. if(sd->state.lr_flag != 2) {
  2166. bonus = status->def2 + val;
  2167. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2168. }
  2169. break;
  2170. case SP_MDEF1:
  2171. if(sd->state.lr_flag != 2) {
  2172. bonus = status->mdef + val;
  2173. #ifdef RENEWAL
  2174. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2175. #else
  2176. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2177. #endif
  2178. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2179. sd->bonus.shieldmdef += bonus;
  2180. }
  2181. }
  2182. break;
  2183. case SP_MDEF2:
  2184. if(sd->state.lr_flag != 2) {
  2185. bonus = status->mdef2 + val;
  2186. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2187. }
  2188. break;
  2189. case SP_HIT:
  2190. if(sd->state.lr_flag != 2) {
  2191. bonus = status->hit + val;
  2192. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2193. } else
  2194. sd->bonus.arrow_hit+=val;
  2195. break;
  2196. case SP_FLEE1:
  2197. if(sd->state.lr_flag != 2) {
  2198. bonus = status->flee + val;
  2199. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2200. }
  2201. break;
  2202. case SP_FLEE2:
  2203. if(sd->state.lr_flag != 2) {
  2204. bonus = status->flee2 + val*10;
  2205. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2206. }
  2207. break;
  2208. case SP_CRITICAL:
  2209. if(sd->state.lr_flag != 2) {
  2210. bonus = status->cri + val*10;
  2211. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2212. } else
  2213. sd->bonus.arrow_cri += val*10;
  2214. break;
  2215. case SP_ATKELE:
  2216. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2217. switch (sd->state.lr_flag)
  2218. {
  2219. case 2:
  2220. switch (sd->status.weapon) {
  2221. case W_BOW:
  2222. case W_REVOLVER:
  2223. case W_RIFLE:
  2224. case W_GATLING:
  2225. case W_SHOTGUN:
  2226. case W_GRENADE:
  2227. //Become weapon element.
  2228. status->rhw.ele=val;
  2229. break;
  2230. default: //Become arrow element.
  2231. sd->bonus.arrow_ele=val;
  2232. break;
  2233. }
  2234. break;
  2235. case 1:
  2236. status->lhw.ele=val;
  2237. break;
  2238. default:
  2239. status->rhw.ele=val;
  2240. break;
  2241. }
  2242. break;
  2243. case SP_DEFELE:
  2244. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2245. if(sd->state.lr_flag != 2)
  2246. status->def_ele=val;
  2247. break;
  2248. case SP_MAXHP:
  2249. if(sd->state.lr_flag == 2)
  2250. break;
  2251. sd->bonus.hp += val;
  2252. break;
  2253. case SP_MAXSP:
  2254. if(sd->state.lr_flag == 2)
  2255. break;
  2256. sd->bonus.sp += val;
  2257. break;
  2258. case SP_MAXHPRATE:
  2259. if(sd->state.lr_flag != 2)
  2260. sd->hprate+=val;
  2261. break;
  2262. case SP_MAXSPRATE:
  2263. if(sd->state.lr_flag != 2)
  2264. sd->sprate+=val;
  2265. break;
  2266. case SP_SPRATE:
  2267. if(sd->state.lr_flag != 2)
  2268. sd->dsprate+=val;
  2269. break;
  2270. case SP_ATTACKRANGE:
  2271. switch (sd->state.lr_flag) {
  2272. case 2:
  2273. switch (sd->status.weapon) {
  2274. case W_BOW:
  2275. case W_REVOLVER:
  2276. case W_RIFLE:
  2277. case W_GATLING:
  2278. case W_SHOTGUN:
  2279. case W_GRENADE:
  2280. status->rhw.range += val;
  2281. }
  2282. break;
  2283. case 1:
  2284. status->lhw.range += val;
  2285. break;
  2286. default:
  2287. status->rhw.range += val;
  2288. break;
  2289. }
  2290. break;
  2291. case SP_SPEED_RATE: //Non stackable increase
  2292. if(sd->state.lr_flag != 2)
  2293. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2294. break;
  2295. case SP_SPEED_ADDRATE: //Stackable increase
  2296. if(sd->state.lr_flag != 2)
  2297. sd->bonus.speed_add_rate -= val;
  2298. break;
  2299. case SP_ASPD: //Raw increase
  2300. if(sd->state.lr_flag != 2)
  2301. sd->bonus.aspd_add -= 10*val;
  2302. break;
  2303. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2304. if(sd->state.lr_flag != 2)
  2305. #ifndef RENEWAL_ASPD
  2306. status->aspd_rate -= 10*val;
  2307. #else
  2308. status->aspd_rate2 += val;
  2309. #endif
  2310. break;
  2311. case SP_HP_RECOV_RATE:
  2312. if(sd->state.lr_flag != 2)
  2313. sd->hprecov_rate += val;
  2314. break;
  2315. case SP_SP_RECOV_RATE:
  2316. if(sd->state.lr_flag != 2)
  2317. sd->sprecov_rate += val;
  2318. break;
  2319. case SP_CRITICAL_DEF:
  2320. if(sd->state.lr_flag != 2)
  2321. sd->bonus.critical_def += val;
  2322. break;
  2323. case SP_NEAR_ATK_DEF:
  2324. if(sd->state.lr_flag != 2)
  2325. sd->bonus.near_attack_def_rate += val;
  2326. break;
  2327. case SP_LONG_ATK_DEF:
  2328. if(sd->state.lr_flag != 2)
  2329. sd->bonus.long_attack_def_rate += val;
  2330. break;
  2331. case SP_DOUBLE_RATE:
  2332. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2333. sd->bonus.double_rate = val;
  2334. break;
  2335. case SP_DOUBLE_ADD_RATE:
  2336. if(sd->state.lr_flag == 0)
  2337. sd->bonus.double_add_rate += val;
  2338. break;
  2339. case SP_MATK_RATE:
  2340. if(sd->state.lr_flag != 2)
  2341. sd->matk_rate += val;
  2342. break;
  2343. case SP_IGNORE_DEF_ELE:
  2344. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2345. if(!sd->state.lr_flag)
  2346. sd->right_weapon.ignore_def_ele |= 1<<val;
  2347. else if(sd->state.lr_flag == 1)
  2348. sd->left_weapon.ignore_def_ele |= 1<<val;
  2349. break;
  2350. case SP_IGNORE_DEF_RACE:
  2351. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2352. if(!sd->state.lr_flag)
  2353. sd->right_weapon.ignore_def_race |= 1<<val;
  2354. else if(sd->state.lr_flag == 1)
  2355. sd->left_weapon.ignore_def_race |= 1<<val;
  2356. break;
  2357. case SP_IGNORE_DEF_CLASS:
  2358. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2359. if(!sd->state.lr_flag)
  2360. sd->right_weapon.ignore_def_class |= 1<<val;
  2361. else if(sd->state.lr_flag == 1)
  2362. sd->left_weapon.ignore_def_class |= 1<<val;
  2363. break;
  2364. case SP_ATK_RATE:
  2365. if(sd->state.lr_flag != 2)
  2366. sd->bonus.atk_rate += val;
  2367. break;
  2368. case SP_MAGIC_ATK_DEF:
  2369. if(sd->state.lr_flag != 2)
  2370. sd->bonus.magic_def_rate += val;
  2371. break;
  2372. case SP_MISC_ATK_DEF:
  2373. if(sd->state.lr_flag != 2)
  2374. sd->bonus.misc_def_rate += val;
  2375. break;
  2376. case SP_IGNORE_MDEF_ELE:
  2377. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2378. if(sd->state.lr_flag != 2)
  2379. sd->bonus.ignore_mdef_ele |= 1<<val;
  2380. break;
  2381. case SP_IGNORE_MDEF_RACE:
  2382. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2383. if(sd->state.lr_flag != 2)
  2384. sd->bonus.ignore_mdef_race |= 1<<val;
  2385. break;
  2386. case SP_PERFECT_HIT_RATE:
  2387. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2388. sd->bonus.perfect_hit = val;
  2389. break;
  2390. case SP_PERFECT_HIT_ADD_RATE:
  2391. if(sd->state.lr_flag != 2)
  2392. sd->bonus.perfect_hit_add += val;
  2393. break;
  2394. case SP_CRITICAL_RATE:
  2395. if(sd->state.lr_flag != 2)
  2396. sd->critical_rate+=val;
  2397. break;
  2398. case SP_DEF_RATIO_ATK_ELE:
  2399. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2400. if(!sd->state.lr_flag)
  2401. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2402. else if(sd->state.lr_flag == 1)
  2403. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2404. break;
  2405. case SP_DEF_RATIO_ATK_RACE:
  2406. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2407. if(!sd->state.lr_flag)
  2408. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2409. else if(sd->state.lr_flag == 1)
  2410. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2411. break;
  2412. case SP_DEF_RATIO_ATK_CLASS:
  2413. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2414. if(!sd->state.lr_flag)
  2415. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2416. else if(sd->state.lr_flag == 1)
  2417. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2418. break;
  2419. case SP_HIT_RATE:
  2420. if(sd->state.lr_flag != 2)
  2421. sd->hit_rate += val;
  2422. break;
  2423. case SP_FLEE_RATE:
  2424. if(sd->state.lr_flag != 2)
  2425. sd->flee_rate += val;
  2426. break;
  2427. case SP_FLEE2_RATE:
  2428. if(sd->state.lr_flag != 2)
  2429. sd->flee2_rate += val;
  2430. break;
  2431. case SP_DEF_RATE:
  2432. if(sd->state.lr_flag != 2)
  2433. sd->def_rate += val;
  2434. break;
  2435. case SP_DEF2_RATE:
  2436. if(sd->state.lr_flag != 2)
  2437. sd->def2_rate += val;
  2438. break;
  2439. case SP_MDEF_RATE:
  2440. if(sd->state.lr_flag != 2)
  2441. sd->mdef_rate += val;
  2442. break;
  2443. case SP_MDEF2_RATE:
  2444. if(sd->state.lr_flag != 2)
  2445. sd->mdef2_rate += val;
  2446. break;
  2447. case SP_RESTART_FULL_RECOVER:
  2448. if(sd->state.lr_flag != 2)
  2449. sd->special_state.restart_full_recover = 1;
  2450. break;
  2451. case SP_NO_CASTCANCEL:
  2452. if(sd->state.lr_flag != 2)
  2453. sd->special_state.no_castcancel = 1;
  2454. break;
  2455. case SP_NO_CASTCANCEL2:
  2456. if(sd->state.lr_flag != 2)
  2457. sd->special_state.no_castcancel2 = 1;
  2458. break;
  2459. case SP_NO_SIZEFIX:
  2460. if(sd->state.lr_flag != 2)
  2461. sd->special_state.no_sizefix = 1;
  2462. break;
  2463. case SP_NO_MAGIC_DAMAGE:
  2464. if(sd->state.lr_flag == 2)
  2465. break;
  2466. val+= sd->special_state.no_magic_damage;
  2467. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2468. break;
  2469. case SP_NO_WEAPON_DAMAGE:
  2470. if(sd->state.lr_flag == 2)
  2471. break;
  2472. val+= sd->special_state.no_weapon_damage;
  2473. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2474. break;
  2475. case SP_NO_MISC_DAMAGE:
  2476. if(sd->state.lr_flag == 2)
  2477. break;
  2478. val+= sd->special_state.no_misc_damage;
  2479. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2480. break;
  2481. case SP_NO_GEMSTONE:
  2482. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2483. sd->special_state.no_gemstone = 1;
  2484. break;
  2485. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2486. if(sd->state.lr_flag != 2) {
  2487. sd->special_state.intravision = 1;
  2488. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2489. }
  2490. break;
  2491. case SP_NO_KNOCKBACK:
  2492. if(sd->state.lr_flag != 2)
  2493. sd->special_state.no_knockback = 1;
  2494. break;
  2495. case SP_SPLASH_RANGE:
  2496. if(sd->bonus.splash_range < val)
  2497. sd->bonus.splash_range = val;
  2498. break;
  2499. case SP_SPLASH_ADD_RANGE:
  2500. sd->bonus.splash_add_range += val;
  2501. break;
  2502. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2503. if(sd->state.lr_flag != 2)
  2504. sd->bonus.short_weapon_damage_return += val;
  2505. break;
  2506. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2507. if(sd->state.lr_flag != 2)
  2508. sd->bonus.long_weapon_damage_return += val;
  2509. break;
  2510. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2511. if(sd->state.lr_flag != 2)
  2512. sd->bonus.magic_damage_return += val;
  2513. break;
  2514. case SP_ALL_STATS: // [Valaris]
  2515. if(sd->state.lr_flag!=2) {
  2516. sd->param_bonus[SP_STR-SP_STR]+=val;
  2517. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2518. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2519. sd->param_bonus[SP_INT-SP_STR]+=val;
  2520. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2521. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2522. }
  2523. break;
  2524. case SP_AGI_VIT: // [Valaris]
  2525. if(sd->state.lr_flag!=2) {
  2526. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2527. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2528. }
  2529. break;
  2530. case SP_AGI_DEX_STR: // [Valaris]
  2531. if(sd->state.lr_flag!=2) {
  2532. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2533. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2534. sd->param_bonus[SP_STR-SP_STR]+=val;
  2535. }
  2536. break;
  2537. case SP_PERFECT_HIDE: // [Valaris]
  2538. if(sd->state.lr_flag!=2)
  2539. sd->special_state.perfect_hiding=1;
  2540. break;
  2541. case SP_UNBREAKABLE:
  2542. if(sd->state.lr_flag!=2)
  2543. sd->bonus.unbreakable += val;
  2544. break;
  2545. case SP_UNBREAKABLE_WEAPON:
  2546. if(sd->state.lr_flag != 2)
  2547. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2548. break;
  2549. case SP_UNBREAKABLE_ARMOR:
  2550. if(sd->state.lr_flag != 2)
  2551. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2552. break;
  2553. case SP_UNBREAKABLE_HELM:
  2554. if(sd->state.lr_flag != 2)
  2555. sd->bonus.unbreakable_equip |= EQP_HELM;
  2556. break;
  2557. case SP_UNBREAKABLE_SHIELD:
  2558. if(sd->state.lr_flag != 2)
  2559. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2560. break;
  2561. case SP_UNBREAKABLE_GARMENT:
  2562. if(sd->state.lr_flag != 2)
  2563. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2564. break;
  2565. case SP_UNBREAKABLE_SHOES:
  2566. if(sd->state.lr_flag != 2)
  2567. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2568. break;
  2569. case SP_CLASSCHANGE: // [Valaris]
  2570. if(sd->state.lr_flag !=2)
  2571. sd->bonus.classchange=val;
  2572. break;
  2573. case SP_LONG_ATK_RATE:
  2574. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2575. sd->bonus.long_attack_atk_rate+=val;
  2576. break;
  2577. case SP_BREAK_WEAPON_RATE:
  2578. if(sd->state.lr_flag != 2)
  2579. sd->bonus.break_weapon_rate+=val;
  2580. break;
  2581. case SP_BREAK_ARMOR_RATE:
  2582. if(sd->state.lr_flag != 2)
  2583. sd->bonus.break_armor_rate+=val;
  2584. break;
  2585. case SP_ADD_STEAL_RATE:
  2586. if(sd->state.lr_flag != 2)
  2587. sd->bonus.add_steal_rate+=val;
  2588. break;
  2589. case SP_DELAYRATE:
  2590. if(sd->state.lr_flag != 2)
  2591. sd->delayrate+=val;
  2592. break;
  2593. case SP_CRIT_ATK_RATE:
  2594. if(sd->state.lr_flag != 2)
  2595. sd->bonus.crit_atk_rate += val;
  2596. break;
  2597. case SP_NO_REGEN:
  2598. if(sd->state.lr_flag != 2)
  2599. sd->regen.state.block|=val;
  2600. break;
  2601. case SP_UNSTRIPABLE_WEAPON:
  2602. if(sd->state.lr_flag != 2)
  2603. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2604. break;
  2605. case SP_UNSTRIPABLE:
  2606. case SP_UNSTRIPABLE_ARMOR:
  2607. if(sd->state.lr_flag != 2)
  2608. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2609. break;
  2610. case SP_UNSTRIPABLE_HELM:
  2611. if(sd->state.lr_flag != 2)
  2612. sd->bonus.unstripable_equip |= EQP_HELM;
  2613. break;
  2614. case SP_UNSTRIPABLE_SHIELD:
  2615. if(sd->state.lr_flag != 2)
  2616. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2617. break;
  2618. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2619. if(!sd->state.lr_flag) {
  2620. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2621. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2622. } else if(sd->state.lr_flag == 1) {
  2623. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2624. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2625. }
  2626. break;
  2627. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2628. if(!sd->state.lr_flag) {
  2629. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2630. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2631. } else if(sd->state.lr_flag == 1) {
  2632. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2633. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2634. }
  2635. break;
  2636. case SP_SP_GAIN_VALUE:
  2637. if(!sd->state.lr_flag)
  2638. sd->bonus.sp_gain_value += val;
  2639. break;
  2640. case SP_HP_GAIN_VALUE:
  2641. if(!sd->state.lr_flag)
  2642. sd->bonus.hp_gain_value += val;
  2643. break;
  2644. case SP_MAGIC_SP_GAIN_VALUE:
  2645. if(!sd->state.lr_flag)
  2646. sd->bonus.magic_sp_gain_value += val;
  2647. break;
  2648. case SP_MAGIC_HP_GAIN_VALUE:
  2649. if(!sd->state.lr_flag)
  2650. sd->bonus.magic_hp_gain_value += val;
  2651. break;
  2652. case SP_ADD_HEAL_RATE:
  2653. if(sd->state.lr_flag != 2)
  2654. sd->bonus.add_heal_rate += val;
  2655. break;
  2656. case SP_ADD_HEAL2_RATE:
  2657. if(sd->state.lr_flag != 2)
  2658. sd->bonus.add_heal2_rate += val;
  2659. break;
  2660. case SP_ADD_ITEM_HEAL_RATE:
  2661. if(sd->state.lr_flag != 2)
  2662. sd->bonus.itemhealrate2 += val;
  2663. break;
  2664. case SP_EMATK:
  2665. if(sd->state.lr_flag != 2)
  2666. sd->bonus.ematk += val;
  2667. break;
  2668. #ifdef RENEWAL_CAST
  2669. case SP_FIXCASTRATE:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2672. break;
  2673. case SP_ADD_FIXEDCAST:
  2674. if(sd->state.lr_flag != 2)
  2675. sd->bonus.add_fixcast += val;
  2676. break;
  2677. case SP_CASTRATE:
  2678. case SP_VARCASTRATE:
  2679. if(sd->state.lr_flag != 2)
  2680. sd->bonus.varcastrate -= val;
  2681. break;
  2682. case SP_ADD_VARIABLECAST:
  2683. if(sd->state.lr_flag != 2)
  2684. sd->bonus.add_varcast += val;
  2685. break;
  2686. #else
  2687. case SP_ADD_FIXEDCAST:
  2688. case SP_FIXCASTRATE:
  2689. case SP_ADD_VARIABLECAST:
  2690. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2691. break;
  2692. case SP_VARCASTRATE:
  2693. case SP_CASTRATE:
  2694. if(sd->state.lr_flag != 2)
  2695. sd->castrate += val;
  2696. break;
  2697. #endif
  2698. case SP_ADDMAXWEIGHT:
  2699. if (sd->state.lr_flag != 2)
  2700. sd->max_weight += val;
  2701. break;
  2702. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2703. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2704. break;
  2705. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2706. if (sd->state.lr_flag != 2)
  2707. sd->bonus.critical_rangeatk += val*10;
  2708. else
  2709. sd->bonus.arrow_cri += val*10;
  2710. break;
  2711. case SP_WEAPON_ATK_RATE:
  2712. if (sd->state.lr_flag != 2)
  2713. sd->bonus.weapon_atk_rate += val;
  2714. break;
  2715. case SP_WEAPON_MATK_RATE:
  2716. if (sd->state.lr_flag != 2)
  2717. sd->bonus.weapon_matk_rate += val;
  2718. break;
  2719. case SP_NO_MADO_FUEL:
  2720. if (sd->state.lr_flag != 2)
  2721. sd->special_state.no_mado_fuel = 1;
  2722. break;
  2723. default:
  2724. if (running_npc_stat_calc_event) {
  2725. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2726. }
  2727. else if (current_equip_combo_pos > 0) {
  2728. 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);
  2729. }
  2730. else {
  2731. 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);
  2732. }
  2733. break;
  2734. }
  2735. }
  2736. /*==========================================
  2737. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2738. * format: bonus2 bBonusName,type2,val;
  2739. * @param sd
  2740. * @param type Bonus type used by bBonusName
  2741. * @param type2
  2742. * @param val Value that usually for rate or fixed value
  2743. *------------------------------------------*/
  2744. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2745. {
  2746. int i;
  2747. nullpo_retv(sd);
  2748. switch(type){
  2749. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2750. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2751. if(!sd->state.lr_flag)
  2752. sd->right_weapon.addele[type2]+=val;
  2753. else if(sd->state.lr_flag == 1)
  2754. sd->left_weapon.addele[type2]+=val;
  2755. else if(sd->state.lr_flag == 2)
  2756. sd->arrow_addele[type2]+=val;
  2757. break;
  2758. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2759. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2760. if(!sd->state.lr_flag)
  2761. sd->right_weapon.addrace[type2]+=val;
  2762. else if(sd->state.lr_flag == 1)
  2763. sd->left_weapon.addrace[type2]+=val;
  2764. else if(sd->state.lr_flag == 2)
  2765. sd->arrow_addrace[type2]+=val;
  2766. break;
  2767. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2768. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2769. if(!sd->state.lr_flag)
  2770. sd->right_weapon.addclass[type2]+=val;
  2771. else if(sd->state.lr_flag == 1)
  2772. sd->left_weapon.addclass[type2]+=val;
  2773. else if(sd->state.lr_flag == 2)
  2774. sd->arrow_addclass[type2]+=val;
  2775. break;
  2776. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2777. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2778. if(!sd->state.lr_flag)
  2779. sd->right_weapon.addsize[type2]+=val;
  2780. else if(sd->state.lr_flag == 1)
  2781. sd->left_weapon.addsize[type2]+=val;
  2782. else if(sd->state.lr_flag == 2)
  2783. sd->arrow_addsize[type2]+=val;
  2784. break;
  2785. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2786. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2787. if(sd->state.lr_flag != 2)
  2788. sd->subele_script[type2] += val;
  2789. break;
  2790. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2791. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2792. if(sd->state.lr_flag != 2)
  2793. sd->subrace[type2]+=val;
  2794. break;
  2795. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2796. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2797. if(sd->state.lr_flag != 2)
  2798. sd->subclass[type2]+=val;
  2799. break;
  2800. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2801. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2802. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2803. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2804. break;
  2805. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2806. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2807. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2808. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2809. break;
  2810. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2811. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2812. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2813. break;
  2814. }
  2815. if(sd->state.lr_flag == 2)
  2816. break;
  2817. i = sd->reseff[type2]+val;
  2818. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2819. break;
  2820. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2821. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2822. if(sd->state.lr_flag != 2)
  2823. sd->magic_addele_script[type2] += val;
  2824. break;
  2825. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2826. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2827. if(sd->state.lr_flag != 2)
  2828. sd->magic_addrace[type2]+=val;
  2829. break;
  2830. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2831. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2832. if(sd->state.lr_flag != 2)
  2833. sd->magic_addclass[type2]+=val;
  2834. break;
  2835. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2836. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2837. if(sd->state.lr_flag != 2)
  2838. sd->magic_addsize[type2]+=val;
  2839. break;
  2840. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2841. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2842. if(sd->state.lr_flag != 2)
  2843. sd->magic_atk_ele[type2]+=val;
  2844. break;
  2845. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2846. switch (sd->state.lr_flag) {
  2847. case 0: //Right hand
  2848. 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);
  2849. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2850. {
  2851. 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));
  2852. break;
  2853. }
  2854. sd->right_weapon.add_dmg[i].class_ = type2;
  2855. sd->right_weapon.add_dmg[i].rate += val;
  2856. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2857. 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]));
  2858. break;
  2859. case 1: //Left hand
  2860. 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);
  2861. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2862. {
  2863. 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));
  2864. break;
  2865. }
  2866. sd->left_weapon.add_dmg[i].class_ = type2;
  2867. sd->left_weapon.add_dmg[i].rate += val;
  2868. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2869. 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]));
  2870. break;
  2871. }
  2872. break;
  2873. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2874. if(sd->state.lr_flag == 2)
  2875. break;
  2876. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2877. if (i == ARRAYLENGTH(sd->add_mdmg))
  2878. {
  2879. 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));
  2880. break;
  2881. }
  2882. sd->add_mdmg[i].class_ = type2;
  2883. sd->add_mdmg[i].rate += val;
  2884. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2885. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2886. break;
  2887. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2888. if(sd->state.lr_flag == 2)
  2889. break;
  2890. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2891. if (i == ARRAYLENGTH(sd->add_def))
  2892. {
  2893. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2894. break;
  2895. }
  2896. sd->add_def[i].class_ = type2;
  2897. sd->add_def[i].rate += val;
  2898. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2899. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2900. break;
  2901. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2902. if(sd->state.lr_flag == 2)
  2903. break;
  2904. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2905. if (i == ARRAYLENGTH(sd->add_mdef))
  2906. {
  2907. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2908. break;
  2909. }
  2910. sd->add_mdef[i].class_ = type2;
  2911. sd->add_mdef[i].rate += val;
  2912. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2913. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2914. break;
  2915. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2916. if(!sd->state.lr_flag) {
  2917. sd->right_weapon.hp_drain_rate.rate += type2;
  2918. sd->right_weapon.hp_drain_rate.per += val;
  2919. }
  2920. else if(sd->state.lr_flag == 1) {
  2921. sd->left_weapon.hp_drain_rate.rate += type2;
  2922. sd->left_weapon.hp_drain_rate.per += val;
  2923. }
  2924. break;
  2925. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2926. if(!sd->state.lr_flag) {
  2927. sd->right_weapon.sp_drain_rate.rate += type2;
  2928. sd->right_weapon.sp_drain_rate.per += val;
  2929. }
  2930. else if(sd->state.lr_flag == 1) {
  2931. sd->left_weapon.sp_drain_rate.rate += type2;
  2932. sd->left_weapon.sp_drain_rate.per += val;
  2933. }
  2934. break;
  2935. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2936. if(sd->state.lr_flag != 2) {
  2937. sd->bonus.sp_vanish_rate += type2;
  2938. sd->bonus.sp_vanish_per += val;
  2939. }
  2940. break;
  2941. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2942. if(sd->state.lr_flag != 2) {
  2943. sd->bonus.hp_vanish_rate += type2;
  2944. sd->bonus.hp_vanish_per += val;
  2945. }
  2946. break;
  2947. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2948. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2949. sd->bonus.get_zeny_rate = val;
  2950. sd->bonus.get_zeny_num = type2;
  2951. }
  2952. break;
  2953. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  2954. if(sd->state.lr_flag != 2) {
  2955. sd->bonus.get_zeny_rate += val;
  2956. sd->bonus.get_zeny_num += type2;
  2957. }
  2958. break;
  2959. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  2960. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  2961. if(sd->state.lr_flag == 2)
  2962. break;
  2963. sd->weapon_coma_ele[type2] += val;
  2964. sd->special_state.bonus_coma = 1;
  2965. break;
  2966. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  2967. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  2968. if(sd->state.lr_flag == 2)
  2969. break;
  2970. sd->weapon_coma_race[type2] += val;
  2971. sd->special_state.bonus_coma = 1;
  2972. break;
  2973. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  2974. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  2975. if(sd->state.lr_flag == 2)
  2976. break;
  2977. sd->weapon_coma_class[type2] += val;
  2978. sd->special_state.bonus_coma = 1;
  2979. break;
  2980. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  2981. if(sd->state.lr_flag != 2)
  2982. sd->weapon_atk[type2]+=val;
  2983. break;
  2984. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  2985. if(sd->state.lr_flag != 2)
  2986. sd->weapon_damage_rate[type2]+=val;
  2987. break;
  2988. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  2989. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  2990. if(sd->state.lr_flag != 2)
  2991. sd->critaddrace[type2] += val*10;
  2992. break;
  2993. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  2994. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  2995. if(sd->state.lr_flag != 2)
  2996. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  2997. break;
  2998. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  2999. if(sd->state.lr_flag == 2)
  3000. break;
  3001. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3002. if (i == ARRAYLENGTH(sd->skillatk))
  3003. { //Better mention this so the array length can be updated. [Skotlex]
  3004. 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);
  3005. break;
  3006. }
  3007. if (sd->skillatk[i].id == type2)
  3008. sd->skillatk[i].val += val;
  3009. else {
  3010. sd->skillatk[i].id = type2;
  3011. sd->skillatk[i].val = val;
  3012. }
  3013. break;
  3014. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3015. if(sd->state.lr_flag == 2)
  3016. break;
  3017. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3018. if (i == ARRAYLENGTH(sd->skillheal))
  3019. { // Better mention this so the array length can be updated. [Skotlex]
  3020. 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);
  3021. break;
  3022. }
  3023. if (sd->skillheal[i].id == type2)
  3024. sd->skillheal[i].val += val;
  3025. else {
  3026. sd->skillheal[i].id = type2;
  3027. sd->skillheal[i].val = val;
  3028. }
  3029. break;
  3030. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3031. if(sd->state.lr_flag == 2)
  3032. break;
  3033. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3034. if (i == ARRAYLENGTH(sd->skillheal2))
  3035. { // Better mention this so the array length can be updated. [Skotlex]
  3036. 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);
  3037. break;
  3038. }
  3039. if (sd->skillheal2[i].id == type2)
  3040. sd->skillheal2[i].val += val;
  3041. else {
  3042. sd->skillheal2[i].id = type2;
  3043. sd->skillheal2[i].val = val;
  3044. }
  3045. break;
  3046. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3047. if(sd->state.lr_flag == 2)
  3048. break;
  3049. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3050. if (i == ARRAYLENGTH(sd->skillblown))
  3051. { //Better mention this so the array length can be updated. [Skotlex]
  3052. 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);
  3053. break;
  3054. }
  3055. if(sd->skillblown[i].id == type2)
  3056. sd->skillblown[i].val += val;
  3057. else {
  3058. sd->skillblown[i].id = type2;
  3059. sd->skillblown[i].val = val;
  3060. }
  3061. break;
  3062. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3063. if(sd->state.lr_flag != 2) {
  3064. sd->hp_loss.value = type2;
  3065. sd->hp_loss.rate = val;
  3066. }
  3067. break;
  3068. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3069. if(sd->state.lr_flag != 2) {
  3070. sd->hp_regen.value = type2;
  3071. sd->hp_regen.rate = val;
  3072. }
  3073. break;
  3074. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3075. if (sd->state.lr_flag != 2) {
  3076. sd->percent_hp_regen.value = type2;
  3077. sd->percent_hp_regen.rate = val;
  3078. }
  3079. break;
  3080. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3081. if (sd->state.lr_flag != 2) {
  3082. sd->percent_sp_regen.value = type2;
  3083. sd->percent_sp_regen.rate = val;
  3084. }
  3085. break;
  3086. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3087. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3088. if(sd->state.lr_flag != 2)
  3089. sd->right_weapon.addrace2[type2] += val;
  3090. else
  3091. sd->left_weapon.addrace2[type2] += val;
  3092. break;
  3093. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3094. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3095. if(sd->state.lr_flag != 2)
  3096. sd->subsize[type2]+=val;
  3097. break;
  3098. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3099. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3100. if(sd->state.lr_flag != 2)
  3101. sd->subrace2[type2]+=val;
  3102. break;
  3103. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3104. if(sd->state.lr_flag == 2)
  3105. break;
  3106. if (!itemdb_exists(type2)) {
  3107. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3108. break;
  3109. }
  3110. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3111. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3112. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3113. break;
  3114. }
  3115. sd->itemhealrate[i].nameid = type2;
  3116. sd->itemhealrate[i].rate += val;
  3117. break;
  3118. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3119. if (sd->state.lr_flag == 2)
  3120. break;
  3121. if (!type2 || !itemdb_group_exists(type2)) {
  3122. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3123. break;
  3124. }
  3125. pc_itemgrouphealrate(sd, type2, val);
  3126. break;
  3127. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3128. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3129. if(sd->state.lr_flag != 2)
  3130. sd->expaddrace[type2]+=val;
  3131. break;
  3132. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3133. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3134. if(sd->state.lr_flag != 2)
  3135. sd->expaddclass[type2]+=val;
  3136. break;
  3137. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3138. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3139. if(sd->state.lr_flag != 2)
  3140. sd->sp_gain_race[type2]+=val;
  3141. break;
  3142. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3143. if (sd->state.lr_flag != 2)
  3144. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3145. break;
  3146. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3147. if (sd->state.lr_flag != 2)
  3148. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3149. break;
  3150. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3151. if(sd->state.lr_flag != 2) {
  3152. sd->sp_loss.value = type2;
  3153. sd->sp_loss.rate = val;
  3154. }
  3155. break;
  3156. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3157. if(sd->state.lr_flag != 2) {
  3158. sd->sp_regen.value = type2;
  3159. sd->sp_regen.rate = val;
  3160. }
  3161. break;
  3162. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3163. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3164. if(!sd->state.lr_flag) {
  3165. sd->right_weapon.hp_drain_race[type2] += val;
  3166. }
  3167. else if(sd->state.lr_flag == 1) {
  3168. sd->left_weapon.hp_drain_race[type2] += val;
  3169. }
  3170. break;
  3171. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3172. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3173. if(!sd->state.lr_flag) {
  3174. sd->right_weapon.sp_drain_race[type2] += val;
  3175. }
  3176. else if(sd->state.lr_flag == 1) {
  3177. sd->left_weapon.sp_drain_race[type2] += val;
  3178. }
  3179. break;
  3180. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3181. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3182. if(!sd->state.lr_flag) {
  3183. sd->right_weapon.hp_drain_class[type2] += val;
  3184. }
  3185. else if(sd->state.lr_flag == 1) {
  3186. sd->left_weapon.hp_drain_class[type2] += val;
  3187. }
  3188. break;
  3189. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3190. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3191. if(!sd->state.lr_flag) {
  3192. sd->right_weapon.sp_drain_class[type2] += val;
  3193. }
  3194. else if(sd->state.lr_flag == 1) {
  3195. sd->left_weapon.sp_drain_class[type2] += val;
  3196. }
  3197. break;
  3198. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3199. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3200. if(sd->state.lr_flag != 2)
  3201. sd->ignore_mdef_by_race[type2] += val;
  3202. break;
  3203. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3204. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3205. if(sd->state.lr_flag != 2)
  3206. sd->ignore_mdef_by_class[type2] += val;
  3207. break;
  3208. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3209. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3210. if(sd->state.lr_flag != 2)
  3211. sd->ignore_def_by_race[type2] += val;
  3212. break;
  3213. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3214. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3215. if (sd->state.lr_flag != 2)
  3216. sd->ignore_def_by_class[type2] += val;
  3217. break;
  3218. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3219. if(sd->state.lr_flag == 2)
  3220. break;
  3221. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3222. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3223. 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);
  3224. break;
  3225. }
  3226. if (sd->skillusesprate[i].id == type2)
  3227. sd->skillusesprate[i].val += val;
  3228. else {
  3229. sd->skillusesprate[i].id = type2;
  3230. sd->skillusesprate[i].val = val;
  3231. }
  3232. break;
  3233. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3234. if(sd->state.lr_flag == 2)
  3235. break;
  3236. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3237. if (i == ARRAYLENGTH(sd->skillcooldown))
  3238. {
  3239. 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);
  3240. break;
  3241. }
  3242. if (sd->skillcooldown[i].id == type2)
  3243. sd->skillcooldown[i].val += val;
  3244. else {
  3245. sd->skillcooldown[i].id = type2;
  3246. sd->skillcooldown[i].val = val;
  3247. }
  3248. break;
  3249. #ifdef RENEWAL_CAST
  3250. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3251. if(sd->state.lr_flag == 2)
  3252. break;
  3253. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3254. if (i == ARRAYLENGTH(sd->skillfixcast))
  3255. {
  3256. 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);
  3257. break;
  3258. }
  3259. if (sd->skillfixcast[i].id == type2)
  3260. sd->skillfixcast[i].val += val;
  3261. else {
  3262. sd->skillfixcast[i].id = type2;
  3263. sd->skillfixcast[i].val = val;
  3264. }
  3265. break;
  3266. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3267. if(sd->state.lr_flag == 2)
  3268. break;
  3269. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3270. if (i == ARRAYLENGTH(sd->skillvarcast))
  3271. {
  3272. 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);
  3273. break;
  3274. }
  3275. if (sd->skillvarcast[i].id == type2)
  3276. sd->skillvarcast[i].val += val;
  3277. else {
  3278. sd->skillvarcast[i].id = type2;
  3279. sd->skillvarcast[i].val = val;
  3280. }
  3281. break;
  3282. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3283. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3284. if(sd->state.lr_flag == 2)
  3285. break;
  3286. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3287. if (i == ARRAYLENGTH(sd->skillcastrate))
  3288. {
  3289. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3290. break;
  3291. }
  3292. if(sd->skillcastrate[i].id == type2)
  3293. sd->skillcastrate[i].val -= val;
  3294. else {
  3295. sd->skillcastrate[i].id = type2;
  3296. sd->skillcastrate[i].val -= val;
  3297. }
  3298. break;
  3299. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3300. if(sd->state.lr_flag == 2)
  3301. break;
  3302. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3303. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3304. {
  3305. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3306. break;
  3307. }
  3308. if(sd->skillfixcastrate[i].id == type2)
  3309. sd->skillfixcastrate[i].val -= val;
  3310. else {
  3311. sd->skillfixcastrate[i].id = type2;
  3312. sd->skillfixcastrate[i].val -= val;
  3313. }
  3314. break;
  3315. #else
  3316. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3317. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3318. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3319. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3320. break;
  3321. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3322. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3323. if(sd->state.lr_flag == 2)
  3324. break;
  3325. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3326. if (i == ARRAYLENGTH(sd->skillcastrate))
  3327. { //Better mention this so the array length can be updated. [Skotlex]
  3328. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3329. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3330. break;
  3331. }
  3332. if(sd->skillcastrate[i].id == type2)
  3333. sd->skillcastrate[i].val += val;
  3334. else {
  3335. sd->skillcastrate[i].id = type2;
  3336. sd->skillcastrate[i].val = val;
  3337. }
  3338. break;
  3339. #endif
  3340. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3341. if(sd->state.lr_flag == 2)
  3342. break;
  3343. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3344. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3345. 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);
  3346. break;
  3347. }
  3348. if (sd->skillusesp[i].id == type2)
  3349. sd->skillusesp[i].val += val;
  3350. else {
  3351. sd->skillusesp[i].id = type2;
  3352. sd->skillusesp[i].val = val;
  3353. }
  3354. break;
  3355. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3356. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3357. if (i == ARRAYLENGTH(sd->subskill)) {
  3358. 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);
  3359. break;
  3360. }
  3361. if (sd->subskill[i].id == type2)
  3362. sd->subskill[i].val += val;
  3363. else {
  3364. sd->subskill[i].id = type2;
  3365. sd->subskill[i].val = val;
  3366. }
  3367. break;
  3368. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3369. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3370. sd->subdefele[type2] += val;
  3371. break;
  3372. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3373. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3374. sd->coma_class[type2] += val;
  3375. sd->special_state.bonus_coma = 1;
  3376. break;
  3377. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3378. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3379. sd->coma_race[type2] += val;
  3380. sd->special_state.bonus_coma = 1;
  3381. break;
  3382. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3383. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3384. if(sd->state.lr_flag != 2)
  3385. sd->magic_addrace2[type2] += val;
  3386. break;
  3387. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3388. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3389. if (sd->state.lr_flag != 2)
  3390. sd->ignore_mdef_by_race2[type2] += val;
  3391. break;
  3392. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3393. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3394. if (sd->state.lr_flag != 2)
  3395. sd->dropaddrace[type2] += val;
  3396. break;
  3397. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3398. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3399. if (sd->state.lr_flag != 2)
  3400. sd->dropaddclass[type2] += val;
  3401. break;
  3402. default:
  3403. if (running_npc_stat_calc_event) {
  3404. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3405. }
  3406. else if (current_equip_combo_pos > 0) {
  3407. 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);
  3408. }
  3409. else {
  3410. 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);
  3411. }
  3412. break;
  3413. }
  3414. }
  3415. /**
  3416. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3417. * @param sd
  3418. * @param type Bonus type used by bBonusName
  3419. * @param type2
  3420. * @param val Value that usually for rate or fixed value
  3421. */
  3422. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3423. {
  3424. nullpo_retv(sd);
  3425. switch(type){
  3426. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3427. if(sd->state.lr_flag != 2)
  3428. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3429. break;
  3430. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3431. if(sd->state.lr_flag != 2)
  3432. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3433. break;
  3434. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3435. if(sd->state.lr_flag != 2)
  3436. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3437. break;
  3438. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3439. if(sd->state.lr_flag != 2)
  3440. {
  3441. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3442. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3443. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3444. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3445. }
  3446. break;
  3447. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3448. if(sd->state.lr_flag != 2)
  3449. {
  3450. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3451. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3452. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3453. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3454. }
  3455. break;
  3456. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3457. if (sd->state.lr_flag != 2)
  3458. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3459. break;
  3460. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3461. if (sd->state.lr_flag != 2)
  3462. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3463. break;
  3464. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3465. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3466. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3467. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3468. break;
  3469. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3470. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3471. if(sd->state.lr_flag != 2)
  3472. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3473. break;
  3474. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3475. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3476. if( sd->state.lr_flag != 2 )
  3477. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3478. break;
  3479. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3480. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3481. if (sd->state.lr_flag != 2)
  3482. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3483. break;
  3484. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3485. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3486. if (sd->state.lr_flag != 2)
  3487. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3488. break;
  3489. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3490. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3491. if(sd->state.lr_flag != 2) {
  3492. sd->sp_vanish_race[type2].rate += type3;
  3493. sd->sp_vanish_race[type2].per += val;
  3494. }
  3495. break;
  3496. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3497. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3498. if(sd->state.lr_flag != 2) {
  3499. sd->hp_vanish_race[type2].rate += type3;
  3500. sd->hp_vanish_race[type2].per += val;
  3501. }
  3502. break;
  3503. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3504. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3505. if (sd->state.lr_flag == 2)
  3506. break;
  3507. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3508. //if (type3 > sd->norecover_state_race[type2].rate) {
  3509. // sd->norecover_state_race[type2].rate = type3;
  3510. // sd->norecover_state_race[type2].tick = val;
  3511. // break;
  3512. //}
  3513. sd->norecover_state_race[type2].rate = type3;
  3514. sd->norecover_state_race[type2].tick = val;
  3515. break;
  3516. default:
  3517. if (running_npc_stat_calc_event) {
  3518. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3519. }
  3520. else if (current_equip_combo_pos > 0) {
  3521. 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);
  3522. }
  3523. else {
  3524. 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);
  3525. }
  3526. break;
  3527. }
  3528. }
  3529. /**
  3530. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3531. * @param sd
  3532. * @param type Bonus type used by bBonusName
  3533. * @param type2
  3534. * @param type3
  3535. * @param val Value that usually for rate or fixed value
  3536. */
  3537. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3538. {
  3539. nullpo_retv(sd);
  3540. switch(type){
  3541. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3542. if(sd->state.lr_flag != 2)
  3543. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3544. break;
  3545. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3546. if(sd->state.lr_flag != 2)
  3547. 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);
  3548. break;
  3549. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3550. if(sd->state.lr_flag != 2)
  3551. {
  3552. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3553. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3554. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3555. }
  3556. break;
  3557. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3558. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3559. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3560. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3561. break;
  3562. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3563. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3564. if (sd->state.lr_flag != 2)
  3565. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3566. break;
  3567. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3568. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3569. if( sd->state.lr_flag != 2 )
  3570. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3571. break;
  3572. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3573. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3574. if(sd->state.lr_flag == 2)
  3575. break;
  3576. sd->def_set_race[type2].rate = type3;
  3577. sd->def_set_race[type2].tick = type4;
  3578. sd->def_set_race[type2].value = val;
  3579. break;
  3580. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3581. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3582. if(sd->state.lr_flag == 2)
  3583. break;
  3584. sd->mdef_set_race[type2].rate = type3;
  3585. sd->mdef_set_race[type2].tick = type4;
  3586. sd->mdef_set_race[type2].value = val;
  3587. break;
  3588. default:
  3589. if (running_npc_stat_calc_event) {
  3590. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3591. }
  3592. else if (current_equip_combo_pos > 0) {
  3593. 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);
  3594. }
  3595. else {
  3596. 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);
  3597. }
  3598. break;
  3599. }
  3600. }
  3601. /**
  3602. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3603. * @param sd
  3604. * @param type Bonus type used by bBonusName
  3605. * @param type2
  3606. * @param type3
  3607. * @param type4
  3608. * @param val Value that usually for rate or fixed value
  3609. */
  3610. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3611. {
  3612. nullpo_retv(sd);
  3613. switch(type){
  3614. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3615. if(sd->state.lr_flag != 2)
  3616. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3617. break;
  3618. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3619. if(sd->state.lr_flag != 2)
  3620. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3621. break;
  3622. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3623. if(sd->state.lr_flag != 2)
  3624. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3625. break;
  3626. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3627. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3628. if( sd->state.lr_flag != 2 )
  3629. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3630. break;
  3631. default:
  3632. if (running_npc_stat_calc_event) {
  3633. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3634. }
  3635. else if (current_equip_combo_pos > 0) {
  3636. 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);
  3637. }
  3638. else {
  3639. 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);
  3640. }
  3641. break;
  3642. }
  3643. }
  3644. /*==========================================
  3645. * Grants a player a given skill. Flag values are:
  3646. * 0 - Grant permanent skill to be bound to skill tree
  3647. * 1 - Grant an item skill (temporary)
  3648. * 2 - Like 1, except the level granted can stack with previously learned level.
  3649. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3650. *------------------------------------------*/
  3651. bool pc_skill(TBL_PC* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3652. uint16 idx = 0;
  3653. nullpo_ret(sd);
  3654. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3655. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3656. return false;
  3657. }
  3658. if (level > MAX_SKILL_LEVEL) {
  3659. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3660. return false;
  3661. }
  3662. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3663. 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);
  3664. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3665. }
  3666. switch (type) {
  3667. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3668. sd->status.skill[idx].id = skill_id;
  3669. sd->status.skill[idx].lv = level;
  3670. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3671. if (level == 0) { //Remove skill.
  3672. sd->status.skill[idx].id = 0;
  3673. clif_deleteskill(sd,skill_id);
  3674. } else
  3675. clif_addskill(sd,skill_id);
  3676. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3677. status_calc_pc(sd, SCO_NONE);
  3678. break;
  3679. case ADDSKILL_TEMP: //Item bonus skill.
  3680. if (sd->status.skill[idx].id != 0) {
  3681. if (sd->status.skill[idx].lv >= level)
  3682. return true;
  3683. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3684. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3685. } else {
  3686. sd->status.skill[idx].id = skill_id;
  3687. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3688. }
  3689. sd->status.skill[idx].lv = level;
  3690. break;
  3691. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3692. if (sd->status.skill[idx].id != 0) {
  3693. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3694. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3695. } else {
  3696. sd->status.skill[idx].id = skill_id;
  3697. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3698. }
  3699. sd->status.skill[idx].lv += level;
  3700. break;
  3701. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3702. sd->status.skill[idx].id = skill_id;
  3703. sd->status.skill[idx].lv = level;
  3704. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3705. if (level == 0) { //Remove skill.
  3706. sd->status.skill[idx].id = 0;
  3707. clif_deleteskill(sd,skill_id);
  3708. } else
  3709. clif_addskill(sd,skill_id);
  3710. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3711. status_calc_pc(sd, SCO_NONE);
  3712. break;
  3713. default:
  3714. return false;
  3715. }
  3716. return true;
  3717. }
  3718. /*==========================================
  3719. * Append a card to an item ?
  3720. *------------------------------------------*/
  3721. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3722. {
  3723. int i;
  3724. unsigned short nameid;
  3725. nullpo_ret(sd);
  3726. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3727. return 0; //Invalid item index.
  3728. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3729. return 0; //Invalid card index.
  3730. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3731. return 0; // target item missing
  3732. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3733. return 0; // target card missing
  3734. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3735. return 0; // only weapons and armor are allowed
  3736. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3737. return 0; // must be a card
  3738. if( sd->status.inventory[idx_equip].identify == 0 )
  3739. return 0; // target must be identified
  3740. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3741. return 0; // card slots reserved for other purposes
  3742. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3743. return 0; // card cannot be compounded on this item type
  3744. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3745. return 0; // attempted to place shield card on left-hand weapon.
  3746. if( sd->status.inventory[idx_equip].equip != 0 )
  3747. return 0; // item must be unequipped
  3748. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3749. if( i == sd->inventory_data[idx_equip]->slot )
  3750. return 0; // no free slots
  3751. // remember the card id to insert
  3752. nameid = sd->status.inventory[idx_card].nameid;
  3753. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3754. {// failed
  3755. clif_insert_card(sd,idx_equip,idx_card,1);
  3756. }
  3757. else
  3758. {// success
  3759. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3760. sd->status.inventory[idx_equip].card[i] = nameid;
  3761. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3762. clif_insert_card(sd,idx_equip,idx_card,0);
  3763. }
  3764. return 0;
  3765. }
  3766. //
  3767. // Items
  3768. //
  3769. /*==========================================
  3770. * Update buying value by skills
  3771. *------------------------------------------*/
  3772. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3773. {
  3774. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3775. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3776. rate1 = 5+skill*2-((skill==10)? 1:0);
  3777. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3778. rate2 = 5+skill*4;
  3779. if(rate1 < rate2) rate1 = rate2;
  3780. if(rate1)
  3781. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3782. if(val < 0) val = 0;
  3783. if(orig_value > 0 && val < 1) val = 1;
  3784. return val;
  3785. }
  3786. /*==========================================
  3787. * Update selling value by skills
  3788. *------------------------------------------*/
  3789. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3790. {
  3791. int skill,val = orig_value,rate = 0;
  3792. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3793. rate = 5+skill*2-((skill==10)? 1:0);
  3794. if(rate)
  3795. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3796. if(val < 0) val = 0;
  3797. if(orig_value > 0 && val < 1) val = 1;
  3798. return val;
  3799. }
  3800. /*==========================================
  3801. * Checking if we have enough place on inventory for new item
  3802. * Make sure to take 30k as limit (for client I guess)
  3803. * @param sd
  3804. * @param nameid
  3805. * @param amount
  3806. * @return e_chkitem_result
  3807. *------------------------------------------*/
  3808. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3809. {
  3810. int i;
  3811. struct item_data* data;
  3812. nullpo_ret(sd);
  3813. if(amount > MAX_AMOUNT)
  3814. return CHKADDITEM_OVERAMOUNT;
  3815. data = itemdb_search(nameid);
  3816. if(!itemdb_isstackable2(data))
  3817. return CHKADDITEM_NEW;
  3818. if( data->stack.inventory && amount > data->stack.amount )
  3819. return CHKADDITEM_OVERAMOUNT;
  3820. for(i=0;i<MAX_INVENTORY;i++){
  3821. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3822. if(sd->status.inventory[i].nameid==nameid){
  3823. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3824. return CHKADDITEM_OVERAMOUNT;
  3825. return CHKADDITEM_EXIST;
  3826. }
  3827. }
  3828. return CHKADDITEM_NEW;
  3829. }
  3830. /*==========================================
  3831. * Return number of available place in inventory
  3832. * Each non stackable item will reduce place by 1
  3833. * @param sd
  3834. * @return Number of empty slots
  3835. *------------------------------------------*/
  3836. uint8 pc_inventoryblank(struct map_session_data *sd)
  3837. {
  3838. uint8 i, b;
  3839. nullpo_ret(sd);
  3840. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3841. if(sd->status.inventory[i].nameid==0)
  3842. b++;
  3843. }
  3844. return b;
  3845. }
  3846. /**
  3847. * Attempts to remove zeny from player
  3848. * @param sd: Player
  3849. * @param zeny: Zeny removed
  3850. * @param type: Log type
  3851. * @param tsd: (optional) From who to log (if null take sd)
  3852. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3853. */
  3854. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3855. {
  3856. nullpo_retr(2,sd);
  3857. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3858. if( zeny < 0 )
  3859. {
  3860. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3861. return 1;
  3862. }
  3863. if( sd->status.zeny < zeny )
  3864. return 1; //Not enough.
  3865. sd->status.zeny -= zeny;
  3866. clif_updatestatus(sd,SP_ZENY);
  3867. if(!tsd) tsd = sd;
  3868. log_zeny(sd, type, tsd, -zeny);
  3869. if( zeny > 0 && sd->state.showzeny ) {
  3870. char output[255];
  3871. sprintf(output, "Removed %dz.", zeny);
  3872. clif_disp_onlyself(sd,output,strlen(output));
  3873. }
  3874. return 0;
  3875. }
  3876. /**
  3877. * Attempts to give zeny to player
  3878. * @param sd: Player
  3879. * @param type: Log type
  3880. * @param tsd: (optional) From who to log (if null take sd)
  3881. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3882. */
  3883. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3884. {
  3885. nullpo_retr(-1,sd);
  3886. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3887. if( zeny < 0 )
  3888. {
  3889. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3890. return 1;
  3891. }
  3892. if( zeny > MAX_ZENY - sd->status.zeny )
  3893. zeny = MAX_ZENY - sd->status.zeny;
  3894. sd->status.zeny += zeny;
  3895. clif_updatestatus(sd,SP_ZENY);
  3896. if(!tsd) tsd = sd;
  3897. log_zeny(sd, type, tsd, zeny);
  3898. if( zeny > 0 && sd->state.showzeny ) {
  3899. char output[255];
  3900. sprintf(output, "Gained %dz.", zeny);
  3901. clif_disp_onlyself(sd,output,strlen(output));
  3902. }
  3903. return 0;
  3904. }
  3905. /**
  3906. * Attempts to remove Cash Points from player
  3907. * @param sd: Player
  3908. * @param price: Points player has to pay
  3909. * @param points: Points player has
  3910. * @param type: Log type
  3911. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3912. */
  3913. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3914. {
  3915. int cash;
  3916. nullpo_retr(-1,sd);
  3917. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3918. if( price < 0 || points < 0 )
  3919. {
  3920. 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);
  3921. return -2;
  3922. }
  3923. if( points > price )
  3924. {
  3925. 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);
  3926. points = price;
  3927. }
  3928. cash = price-points;
  3929. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3930. {
  3931. 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);
  3932. return -1;
  3933. }
  3934. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints-cash);
  3935. if( cash ){
  3936. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3937. }
  3938. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints-points);
  3939. if( points ){
  3940. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3941. }
  3942. if( battle_config.cashshop_show_points )
  3943. {
  3944. char output[CHAT_SIZE_MAX];
  3945. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3946. clif_disp_onlyself(sd, output, strlen(output));
  3947. }
  3948. return cash+points;
  3949. }
  3950. /**
  3951. * Attempts to give Cash Points to player
  3952. * @param sd: Player
  3953. * @param cash: Cash player gets
  3954. * @param points: Points player has
  3955. * @param type: Log type
  3956. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  3957. */
  3958. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  3959. {
  3960. char output[CHAT_SIZE_MAX];
  3961. nullpo_retr(-1,sd);
  3962. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3963. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3964. if( cash > 0 )
  3965. {
  3966. if( cash > MAX_ZENY-sd->cashPoints )
  3967. {
  3968. 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);
  3969. cash = MAX_ZENY-sd->cashPoints;
  3970. }
  3971. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints+cash);
  3972. if( cash ){
  3973. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3974. }
  3975. if( battle_config.cashshop_show_points )
  3976. {
  3977. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3978. clif_disp_onlyself(sd, output, strlen(output));
  3979. }
  3980. return cash;
  3981. }
  3982. else if( cash < 0 )
  3983. {
  3984. 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);
  3985. return -1;
  3986. }
  3987. if( points > 0 )
  3988. {
  3989. if( points > MAX_ZENY-sd->kafraPoints )
  3990. {
  3991. 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);
  3992. points = MAX_ZENY-sd->kafraPoints;
  3993. }
  3994. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints+points);
  3995. if( points ){
  3996. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3997. }
  3998. if( battle_config.cashshop_show_points )
  3999. {
  4000. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4001. clif_disp_onlyself(sd, output, strlen(output));
  4002. }
  4003. return points;
  4004. }
  4005. else if( points < 0 )
  4006. {
  4007. 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);
  4008. return -1;
  4009. }
  4010. return -2; //shouldn't happen but just in case
  4011. }
  4012. /**
  4013. * Searching a specified itemid in inventory and return his stored index
  4014. * @param sd Player
  4015. * @param nameid Find this Item!
  4016. * @return Stored index in inventory, or -1 if not found.
  4017. **/
  4018. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4019. short i;
  4020. nullpo_retr(-1, sd);
  4021. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && (sd->status.inventory[i].amount > 0 || nameid == 0) );
  4022. return ( i < MAX_INVENTORY ) ? i : -1;
  4023. }
  4024. /** Attempt to add a new item to player inventory
  4025. * @param sd
  4026. * @param item_data
  4027. * @param amount
  4028. * @param log_type
  4029. * @return
  4030. * 0 = success
  4031. * 1 = invalid itemid not found or negative amount
  4032. * 2 = overweight
  4033. * 3 = ?
  4034. * 4 = no free place found
  4035. * 5 = max amount reached
  4036. * 6 = ?
  4037. * 7 = stack limitation
  4038. */
  4039. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4040. struct item_data *id;
  4041. int16 i;
  4042. unsigned int w;
  4043. nullpo_retr(1, sd);
  4044. nullpo_retr(1, item);
  4045. if( item->nameid == 0 || amount <= 0 )
  4046. return ADDITEM_INVALID;
  4047. if( amount > MAX_AMOUNT )
  4048. return ADDITEM_OVERAMOUNT;
  4049. id = itemdb_search(item->nameid);
  4050. if( id->stack.inventory && amount > id->stack.amount )
  4051. {// item stack limitation
  4052. return ADDITEM_STACKLIMIT;
  4053. }
  4054. w = id->weight*amount;
  4055. if(sd->weight + w > sd->max_weight)
  4056. return ADDITEM_OVERWEIGHT;
  4057. i = MAX_INVENTORY;
  4058. if (id->flag.guid && !item->unique_id)
  4059. item->unique_id = pc_generate_unique_id(sd);
  4060. // Stackable | Non Rental
  4061. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4062. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4063. if( sd->status.inventory[i].nameid == item->nameid &&
  4064. sd->status.inventory[i].bound == item->bound &&
  4065. sd->status.inventory[i].expire_time == 0 &&
  4066. sd->status.inventory[i].unique_id == item->unique_id &&
  4067. memcmp(&sd->status.inventory[i].card, &item->card, sizeof(item->card)) == 0
  4068. )
  4069. {
  4070. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->status.inventory[i].amount ) )
  4071. return ADDITEM_OVERAMOUNT;
  4072. sd->status.inventory[i].amount += amount;
  4073. clif_additem(sd,i,amount,0);
  4074. break;
  4075. }
  4076. }
  4077. }
  4078. if (i >= MAX_INVENTORY) {
  4079. i = pc_search_inventory(sd,0);
  4080. if( i < 0 )
  4081. return ADDITEM_OVERITEM;
  4082. memcpy(&sd->status.inventory[i], item, sizeof(sd->status.inventory[0]));
  4083. // clear equip and favorite fields first, just in case
  4084. if( item->equip )
  4085. sd->status.inventory[i].equip = 0;
  4086. if( item->favorite )
  4087. sd->status.inventory[i].favorite = 0;
  4088. sd->status.inventory[i].amount = amount;
  4089. sd->inventory_data[i] = id;
  4090. sd->last_addeditem_index = i;
  4091. if (!itemdb_isstackable2(id) || id->flag.guid)
  4092. sd->status.inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4093. clif_additem(sd,i,amount,0);
  4094. }
  4095. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  4096. sd->weight += w;
  4097. clif_updatestatus(sd,SP_WEIGHT);
  4098. //Auto-equip
  4099. if(id->flag.autoequip)
  4100. pc_equipitem(sd, i, id->equip);
  4101. /* rental item check */
  4102. if( item->expire_time ) {
  4103. if( time(NULL) > item->expire_time ) {
  4104. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  4105. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4106. } else {
  4107. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4108. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  4109. pc_inventory_rental_add(sd, seconds);
  4110. }
  4111. }
  4112. return ADDITEM_SUCCESS;
  4113. }
  4114. /*==========================================
  4115. * Remove an item at index n from inventory by amount.
  4116. * @param sd
  4117. * @param n Item index in inventory
  4118. * @param amount
  4119. * @param type &1: Don't notify deletion; &2 Don't notify weight change
  4120. * @param reason Delete reason
  4121. * @param log_type e_log_pick_type
  4122. * @return 1 - invalid itemid or negative amount; 0 - Success
  4123. *------------------------------------------*/
  4124. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4125. {
  4126. nullpo_retr(1, sd);
  4127. if(n < 0 || sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4128. return 1;
  4129. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  4130. sd->status.inventory[n].amount -= amount;
  4131. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4132. if( sd->status.inventory[n].amount <= 0 ){
  4133. if(sd->status.inventory[n].equip)
  4134. pc_unequipitem(sd,n,3);
  4135. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  4136. sd->inventory_data[n] = NULL;
  4137. }
  4138. if(!(type&1))
  4139. clif_delitem(sd,n,amount,reason);
  4140. if(!(type&2))
  4141. clif_updatestatus(sd,SP_WEIGHT);
  4142. return 0;
  4143. }
  4144. /*==========================================
  4145. * Attempt to drop an item.
  4146. * @param sd
  4147. * @param n Item index in inventory
  4148. * @param amount Amount of item
  4149. * @return False = fail; True = success
  4150. *------------------------------------------*/
  4151. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4152. {
  4153. nullpo_retr(1, sd);
  4154. if(n < 0 || n >= MAX_INVENTORY)
  4155. return false;
  4156. if(amount <= 0)
  4157. return false;
  4158. if(sd->status.inventory[n].nameid <= 0 ||
  4159. sd->status.inventory[n].amount <= 0 ||
  4160. sd->status.inventory[n].amount < amount ||
  4161. sd->state.trading || sd->state.vending ||
  4162. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4163. )
  4164. return false;
  4165. if( map[sd->bl.m].flag.nodrop )
  4166. {
  4167. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4168. return false; //Can't drop items in nodrop mapflag maps.
  4169. }
  4170. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  4171. {
  4172. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4173. return false;
  4174. }
  4175. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4176. return false;
  4177. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4178. clif_dropitem(sd, n, amount);
  4179. return true;
  4180. }
  4181. /*==========================================
  4182. * Attempt to pick up an item.
  4183. * @param sd
  4184. * @param fitem Item that will be picked
  4185. * @return False = fail; True = success
  4186. *------------------------------------------*/
  4187. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4188. {
  4189. int flag = 0;
  4190. unsigned int tick = gettick();
  4191. struct party_data *p = NULL;
  4192. nullpo_ret(sd);
  4193. nullpo_ret(fitem);
  4194. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4195. return false; // Distance is too far
  4196. if (sd->sc.cant.pickup)
  4197. return false;
  4198. if (sd->status.party_id)
  4199. p = party_search(sd->status.party_id);
  4200. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4201. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4202. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4203. if (!(p && p->party.item&1 &&
  4204. first_sd && first_sd->status.party_id == sd->status.party_id
  4205. ))
  4206. return false;
  4207. }
  4208. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4209. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4210. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4211. if (!(p && p->party.item&1 &&
  4212. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4213. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4214. ))
  4215. return false;
  4216. }
  4217. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4218. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4219. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4220. if(!(p && p->party.item&1 &&
  4221. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4222. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4223. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4224. ))
  4225. return false;
  4226. }
  4227. }
  4228. }
  4229. }
  4230. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4231. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4232. clif_additem(sd,0,0,flag);
  4233. return true;
  4234. }
  4235. //Display pickup animation.
  4236. pc_stop_attack(sd);
  4237. clif_takeitem(&sd->bl,&fitem->bl);
  4238. if (fitem->mob_id &&
  4239. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4240. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4241. )
  4242. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4243. map_clearflooritem(&fitem->bl);
  4244. return true;
  4245. }
  4246. /*==========================================
  4247. * Check if item is usable.
  4248. * Return:
  4249. * false = no
  4250. * true = yes
  4251. *------------------------------------------*/
  4252. bool pc_isUseitem(struct map_session_data *sd,int n)
  4253. {
  4254. struct item_data *item;
  4255. unsigned short nameid;
  4256. nullpo_ret(sd);
  4257. item = sd->inventory_data[n];
  4258. nameid = sd->status.inventory[n].nameid;
  4259. if( item == NULL )
  4260. return false;
  4261. //Not consumable item
  4262. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4263. return false;
  4264. if( !item->script ) //if it has no script, you can't really consume it!
  4265. return false;
  4266. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4267. return true;
  4268. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4269. return false;
  4270. //Prevent mass item usage. [Skotlex]
  4271. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4272. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4273. )
  4274. return false;
  4275. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4276. clif_msg(sd,ITEM_NOUSE_SITTING);
  4277. return false; // You cannot use this item while sitting.
  4278. }
  4279. if (sd->state.storage_flag && item->type != IT_CASH) {
  4280. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd,388));
  4281. return false; // You cannot use this item while storage is open.
  4282. }
  4283. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m)))
  4284. return false;
  4285. switch( nameid ) {
  4286. case ITEMID_ANODYNE:
  4287. if( map_flag_gvg(sd->bl.m) )
  4288. return false;
  4289. break;
  4290. case ITEMID_WING_OF_FLY:
  4291. case ITEMID_GIANT_FLY_WING:
  4292. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) ) {
  4293. clif_skill_teleportmessage(sd,0);
  4294. return false;
  4295. }
  4296. case ITEMID_WING_OF_BUTTERFLY:
  4297. case ITEMID_DUN_TELE_SCROLL1:
  4298. case ITEMID_DUN_TELE_SCROLL2:
  4299. case ITEMID_DUN_TELE_SCROLL3:
  4300. case ITEMID_WOB_RUNE:
  4301. case ITEMID_WOB_SCHWALTZ:
  4302. case ITEMID_WOB_RACHEL:
  4303. case ITEMID_WOB_LOCAL:
  4304. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4305. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4306. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4307. return false;
  4308. }
  4309. if( nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && map[sd->bl.m].flag.noreturn )
  4310. return false;
  4311. break;
  4312. case ITEMID_BUBBLE_GUM:
  4313. case ITEMID_COMP_BUBBLE_GUM:
  4314. if( sd->sc.data[SC_ITEMBOOST] )
  4315. return false;
  4316. break;
  4317. case ITEMID_BATTLE_MANUAL:
  4318. case ITEMID_COMP_BATTLE_MANUAL:
  4319. case ITEMID_THICK_BATTLE_MANUAL:
  4320. case ITEMID_NOBLE_NAMEPLATE:
  4321. case ITEMID_BATTLE_MANUAL25:
  4322. case ITEMID_BATTLE_MANUAL100:
  4323. case ITEMID_BATTLE_MANUAL300:
  4324. if( sd->sc.data[SC_EXPBOOST] )
  4325. return false;
  4326. break;
  4327. case ITEMID_JOB_MANUAL50:
  4328. if( sd->sc.data[SC_JEXPBOOST] )
  4329. return false;
  4330. break;
  4331. case ITEMID_MERCENARY_RED_POTION:
  4332. case ITEMID_MERCENARY_BLUE_POTION:
  4333. case ITEMID_M_CENTER_POTION:
  4334. case ITEMID_M_AWAKENING_POTION:
  4335. case ITEMID_M_BERSERK_POTION:
  4336. if( sd->md == NULL || sd->md->db == NULL )
  4337. return false;
  4338. if( sd->md->sc.data[SC_BERSERK] )
  4339. return false;
  4340. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4341. return false;
  4342. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4343. return false;
  4344. break;
  4345. case ITEMID_NEURALIZER:
  4346. if( !map[sd->bl.m].flag.reset )
  4347. return false;
  4348. break;
  4349. }
  4350. if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL )
  4351. return false; // Mercenary Scrolls
  4352. /**
  4353. * Only Rune Knights may use runes
  4354. **/
  4355. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  4356. return false;
  4357. /**
  4358. * Only GCross may use poisons
  4359. **/
  4360. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  4361. return false;
  4362. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4363. if( pc_is90overweight(sd) ) {
  4364. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4365. return false;
  4366. }
  4367. if( !pc_inventoryblank(sd) ) {
  4368. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 732)); //Item cannot be open when inventory is full
  4369. return false;
  4370. }
  4371. }
  4372. //Gender check
  4373. if(item->sex != 2 && sd->status.sex != item->sex)
  4374. return false;
  4375. //Required level check
  4376. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4377. return false;
  4378. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4379. return false;
  4380. //Not equipable by class. [Skotlex]
  4381. if (!(
  4382. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4383. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4384. ))
  4385. return false;
  4386. if (sd->sc.count && (
  4387. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4388. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4389. sd->sc.data[SC_TRICKDEAD] ||
  4390. sd->sc.data[SC_HIDING] ||
  4391. sd->sc.data[SC__SHADOWFORM] ||
  4392. sd->sc.data[SC__INVISIBILITY] ||
  4393. sd->sc.data[SC__MANHOLE] ||
  4394. sd->sc.data[SC_KAGEHUMI] ||
  4395. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4396. sd->sc.data[SC_HEAT_BARREL_AFTER] ||
  4397. sd->sc.data[SC_KINGS_GRACE] ||
  4398. sd->sc.data[SC_SUHIDE]))
  4399. return false;
  4400. if (!pc_isItemClass(sd,item))
  4401. return false;
  4402. //Dead Branch items
  4403. if( item->flag.dead_branch )
  4404. log_branch(sd);
  4405. return true;
  4406. }
  4407. /*==========================================
  4408. * Last checks to use an item.
  4409. * Return:
  4410. * 0 = fail
  4411. * 1 = success
  4412. *------------------------------------------*/
  4413. int pc_useitem(struct map_session_data *sd,int n)
  4414. {
  4415. unsigned int tick = gettick();
  4416. int amount;
  4417. unsigned short nameid;
  4418. struct script_code *script;
  4419. struct item item;
  4420. struct item_data *id;
  4421. nullpo_ret(sd);
  4422. if (sd->npc_id) {
  4423. if (sd->progressbar.npc_id) {
  4424. clif_progressbar_abort(sd);
  4425. return 0; // First item use attempt cancels the progress bar
  4426. }
  4427. #ifdef RENEWAL
  4428. if (pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  4429. clif_msg(sd, WORK_IN_PROGRESS);
  4430. return 0;
  4431. }
  4432. #else
  4433. if (!sd->npc_item_flag)
  4434. return 0;
  4435. #endif
  4436. }
  4437. item = sd->status.inventory[n];
  4438. id = sd->inventory_data[n];
  4439. if (item.nameid == 0 || item.amount <= 0)
  4440. return 0;
  4441. if( !pc_isUseitem(sd,n) )
  4442. return 0;
  4443. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4444. nameid = id->nameid;
  4445. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4446. return 0;
  4447. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4448. if( id->flag.delay_consume ) {
  4449. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4450. return 0;
  4451. else if( pc_issit(sd) )
  4452. return 0;
  4453. }
  4454. //Since most delay-consume items involve using a "skill-type" target cursor,
  4455. //perform a skill-use check before going through. [Skotlex]
  4456. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4457. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4458. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4459. return 0;
  4460. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4461. return 0;
  4462. /* on restricted maps the item is consumed but the effect is not used */
  4463. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4464. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4465. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4466. clif_useitemack(sd,n,item.amount-1,true);
  4467. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4468. }
  4469. return 0;/* regardless, effect is not run */
  4470. }
  4471. sd->itemid = item.nameid;
  4472. sd->itemindex = n;
  4473. if(sd->catch_target_class != -1) //Abort pet catching.
  4474. sd->catch_target_class = -1;
  4475. amount = item.amount;
  4476. script = id->script;
  4477. //Check if the item is to be consumed immediately [Skotlex]
  4478. if (id->flag.delay_consume)
  4479. clif_useitemack(sd, n, amount, true);
  4480. else
  4481. {
  4482. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4483. {
  4484. clif_useitemack(sd, n, amount - 1, true);
  4485. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4486. }
  4487. else
  4488. clif_useitemack(sd, n, 0, false);
  4489. }
  4490. if(item.card[0]==CARD0_CREATE &&
  4491. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4492. {
  4493. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4494. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4495. potion_flag = 3; //Even more effective potions.
  4496. }
  4497. //Update item use time.
  4498. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4499. if( itemdb_iscashfood(nameid) )
  4500. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4501. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4502. potion_flag = 0;
  4503. return 1;
  4504. }
  4505. /**
  4506. * Add item on cart for given index.
  4507. * @param sd
  4508. * @param item
  4509. * @param amount
  4510. * @param log_type
  4511. * @return 0 = success; 1 = fail; 2 = no slot
  4512. */
  4513. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4514. {
  4515. struct item_data *data;
  4516. int i,w;
  4517. nullpo_retr(1, sd);
  4518. nullpo_retr(1, item);
  4519. if(item->nameid == 0 || amount <= 0)
  4520. return 1;
  4521. data = itemdb_search(item->nameid);
  4522. if( data->stack.cart && amount > data->stack.amount )
  4523. {// item stack limitation
  4524. return 1;
  4525. }
  4526. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4527. { // Check item trade restrictions [Skotlex]
  4528. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4529. return 1;
  4530. }
  4531. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4532. return 1;
  4533. // ID no longer points to inventory/kafra ID. While we get a new one we don't want to mess up vending creation.
  4534. item->id = 0;
  4535. i = MAX_CART;
  4536. if( itemdb_isstackable2(data) && !item->expire_time )
  4537. {
  4538. for (i = 0; i < MAX_CART; i++) {
  4539. if (sd->status.cart[i].nameid == item->nameid
  4540. && sd->status.cart[i].bound == item->bound
  4541. && sd->status.cart[i].unique_id == item->unique_id
  4542. && memcmp(sd->status.cart[i].card, item->card, sizeof(item->card)) == 0
  4543. )
  4544. break;
  4545. }
  4546. }
  4547. if( i < MAX_CART )
  4548. {// item already in cart, stack it
  4549. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4550. return 2; // no slot
  4551. sd->status.cart[i].amount+=amount;
  4552. clif_cart_additem(sd,i,amount,0);
  4553. }
  4554. else
  4555. {// item not stackable or not present, add it
  4556. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4557. if( i == MAX_CART )
  4558. return 2; // no slot
  4559. memcpy(&sd->status.cart[i],item,sizeof(sd->status.cart[0]));
  4560. sd->status.cart[i].amount=amount;
  4561. sd->cart_num++;
  4562. clif_cart_additem(sd,i,amount,0);
  4563. }
  4564. sd->status.cart[i].favorite = 0;/* clear */
  4565. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4566. sd->cart_weight += w;
  4567. clif_updatestatus(sd,SP_CARTINFO);
  4568. return 0;
  4569. }
  4570. /*==========================================
  4571. * Delete item on cart for given index.
  4572. *------------------------------------------*/
  4573. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4574. {
  4575. nullpo_retv(sd);
  4576. if(sd->status.cart[n].nameid == 0 ||
  4577. sd->status.cart[n].amount<amount)
  4578. return;
  4579. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4580. sd->status.cart[n].amount -= amount;
  4581. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4582. if(sd->status.cart[n].amount <= 0){
  4583. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4584. sd->cart_num--;
  4585. }
  4586. if(!type) {
  4587. clif_cart_delitem(sd,n,amount);
  4588. clif_updatestatus(sd,SP_CARTINFO);
  4589. }
  4590. }
  4591. /*==========================================
  4592. * Transfer item from inventory to cart.
  4593. *------------------------------------------*/
  4594. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4595. {
  4596. struct item *item_data;
  4597. char flag;
  4598. nullpo_retv(sd);
  4599. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4600. return;
  4601. item_data = &sd->status.inventory[idx];
  4602. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4603. return;
  4604. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4605. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4606. else {
  4607. clif_dropitem(sd,idx,0);
  4608. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4609. }
  4610. }
  4611. /*==========================================
  4612. * Get number of item in cart.
  4613. * Return:
  4614. -1 = itemid not found or no amount found
  4615. x = remaining itemid on cart after get
  4616. *------------------------------------------*/
  4617. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4618. {
  4619. struct item* item_data;
  4620. nullpo_retr(-1, sd);
  4621. item_data = &sd->status.cart[idx];
  4622. if( item_data->nameid == 0 || item_data->amount == 0 )
  4623. return -1;
  4624. return item_data->amount - amount;
  4625. }
  4626. /*==========================================
  4627. * Retrieve an item at index idx from cart.
  4628. *------------------------------------------*/
  4629. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4630. {
  4631. struct item *item_data;
  4632. unsigned char flag = 0;
  4633. nullpo_retv(sd);
  4634. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4635. return;
  4636. item_data=&sd->status.cart[idx];
  4637. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4638. return;
  4639. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4640. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4641. else {
  4642. clif_dropitem(sd,idx,0);
  4643. clif_additem(sd,0,0,flag);
  4644. }
  4645. }
  4646. /*==========================================
  4647. * Bound Item Check
  4648. * Type:
  4649. * 1 Account Bound
  4650. * 2 Guild Bound
  4651. * 3 Party Bound
  4652. * 4 Character Bound
  4653. *------------------------------------------*/
  4654. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4655. {
  4656. int i=0, j=0;
  4657. for(i=0;i<MAX_INVENTORY;i++){
  4658. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4659. idxlist[j] = i;
  4660. j++;
  4661. }
  4662. }
  4663. return j;
  4664. }
  4665. /*==========================================
  4666. * Display item stolen msg to player sd
  4667. *------------------------------------------*/
  4668. int pc_show_steal(struct block_list *bl,va_list ap)
  4669. {
  4670. struct map_session_data *sd;
  4671. int itemid;
  4672. struct item_data *item=NULL;
  4673. char output[100];
  4674. sd=va_arg(ap,struct map_session_data *);
  4675. itemid=va_arg(ap,int);
  4676. if((item=itemdb_exists(itemid))==NULL)
  4677. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4678. else
  4679. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4680. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4681. return 0;
  4682. }
  4683. /*==========================================
  4684. * Steal an item from bl (mob).
  4685. * Return:
  4686. * 0 = fail
  4687. * 1 = succes
  4688. *------------------------------------------*/
  4689. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4690. {
  4691. int i,itemid;
  4692. double rate;
  4693. unsigned char flag = 0;
  4694. struct status_data *sd_status, *md_status;
  4695. struct mob_data *md;
  4696. struct item tmp_item;
  4697. if(!sd || !bl || bl->type!=BL_MOB)
  4698. return 0;
  4699. md = (TBL_MOB *)bl;
  4700. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE ) ) //already stolen from / status change check
  4701. return 0;
  4702. sd_status= status_get_status_data(&sd->bl);
  4703. md_status= status_get_status_data(bl);
  4704. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4705. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4706. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4707. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4708. ) { //Can't steal from
  4709. md->state.steal_flag = UCHAR_MAX;
  4710. return 0;
  4711. }
  4712. // base skill success chance (percentual)
  4713. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4714. rate += sd->bonus.add_steal_rate;
  4715. if( rate < 1 )
  4716. return 0;
  4717. // Try dropping one item, in the order from first to last possible slot.
  4718. // Droprate is affected by the skill success rate.
  4719. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4720. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4721. break;
  4722. if( i == MAX_STEAL_DROP )
  4723. return 0;
  4724. itemid = md->db->dropitem[i].nameid;
  4725. memset(&tmp_item,0,sizeof(tmp_item));
  4726. tmp_item.nameid = itemid;
  4727. tmp_item.amount = 1;
  4728. tmp_item.identify = itemdb_isidentified(itemid);
  4729. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4730. //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?
  4731. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4732. if(flag) { //Failed to steal due to overweight
  4733. clif_additem(sd,0,0,flag);
  4734. return 0;
  4735. }
  4736. if(battle_config.show_steal_in_same_party)
  4737. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4738. //Logs items, Stolen from mobs [Lupus]
  4739. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4740. //A Rare Steal Global Announce by Lupus
  4741. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4742. struct item_data *i_data;
  4743. char message[128];
  4744. i_data = itemdb_search(itemid);
  4745. sprintf (message, msg_txt(sd,542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4746. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4747. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4748. }
  4749. return 1;
  4750. }
  4751. /*==========================================
  4752. * Stole zeny from bl (mob)
  4753. * return
  4754. * 0 = fail
  4755. * 1 = success
  4756. *------------------------------------------*/
  4757. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4758. {
  4759. int rate,skill;
  4760. struct mob_data *md;
  4761. if(!sd || !target || target->type != BL_MOB)
  4762. return 0;
  4763. md = (TBL_MOB*)target;
  4764. 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)
  4765. return 0;
  4766. // FIXME: This formula is either custom or outdated.
  4767. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4768. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4769. if(rnd()%1000 < rate)
  4770. {
  4771. int amount = md->level*10 + rnd()%100;
  4772. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4773. md->state.steal_coin_flag = 1;
  4774. return 1;
  4775. }
  4776. return 0;
  4777. }
  4778. /*==========================================
  4779. * Set's a player position.
  4780. * @param sd
  4781. * @param mapindex
  4782. * @param x
  4783. * @param y
  4784. * @param clrtype
  4785. * @return SETPOS_OK Success
  4786. * SETPOS_MAPINDEX Invalid map index
  4787. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4788. * SETPOS_AUTOTRADE Player is in autotrade state
  4789. *------------------------------------------*/
  4790. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4791. {
  4792. int16 m;
  4793. nullpo_retr(SETPOS_OK,sd);
  4794. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4795. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4796. return SETPOS_MAPINDEX;
  4797. }
  4798. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4799. return SETPOS_AUTOTRADE;
  4800. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4801. pc_setstand(sd, true);
  4802. pc_setrestartvalue(sd,1);
  4803. }
  4804. m = map_mapindex2mapid(mapindex);
  4805. sd->state.changemap = (sd->mapindex != mapindex);
  4806. sd->state.warping = 1;
  4807. sd->state.workinprogress = WIP_DISABLE_NONE;
  4808. if(map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4809. bool instance_found = false;
  4810. struct party_data *p = NULL;
  4811. struct guild *g = NULL;
  4812. if (sd->instance_id) {
  4813. instance_delusers(sd->instance_id);
  4814. instance_found = true;
  4815. }
  4816. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4817. instance_delusers(p->instance_id);
  4818. instance_found = true;
  4819. }
  4820. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4821. instance_delusers(g->instance_id);
  4822. }
  4823. if( sd->state.changemap ) { // Misc map-changing settings
  4824. int i;
  4825. sd->state.pmap = sd->bl.m;
  4826. if (sd->sc.count) { // Cancel some map related stuff.
  4827. if (sd->sc.data[SC_JAILED])
  4828. return SETPOS_MAPINDEX; //You may not get out!
  4829. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4830. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4831. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4832. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4833. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4834. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4835. if (sd->sc.data[SC_KNOWLEDGE]) {
  4836. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4837. if (sce->timer != INVALID_TIMER)
  4838. delete_timer(sce->timer, status_change_timer);
  4839. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4840. }
  4841. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4842. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4843. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4844. }
  4845. for( i = 0; i < EQI_MAX; i++ ) {
  4846. if( sd->equip_index[i] >= 0 )
  4847. if( pc_isequip(sd,sd->equip_index[i]) )
  4848. pc_unequipitem(sd,sd->equip_index[i],2);
  4849. }
  4850. if (battle_config.clear_unit_onwarp&BL_PC)
  4851. skill_clear_unitgroup(&sd->bl);
  4852. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4853. guild_send_dot_remove(sd);
  4854. bg_send_dot_remove(sd);
  4855. if (sd->regen.state.gc)
  4856. sd->regen.state.gc = 0;
  4857. // make sure vending is allowed here
  4858. if (sd->state.vending && map[m].flag.novending) {
  4859. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4860. vending_closevending(sd);
  4861. }
  4862. channel_pcquit(sd,4); //quit map chan
  4863. }
  4864. if( m < 0 )
  4865. {
  4866. uint32 ip;
  4867. uint16 port;
  4868. //if can't find any map-servers, just abort setting position.
  4869. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4870. return SETPOS_NO_MAPSERVER;
  4871. if (sd->npc_id)
  4872. npc_event_dequeue(sd);
  4873. npc_script_event(sd, NPCE_LOGOUT);
  4874. //remove from map, THEN change x/y coordinates
  4875. unit_remove_map_pc(sd,clrtype);
  4876. sd->mapindex = mapindex;
  4877. sd->bl.x=x;
  4878. sd->bl.y=y;
  4879. pc_clean_skilltree(sd);
  4880. chrif_save(sd,2);
  4881. chrif_changemapserver(sd, ip, (short)port);
  4882. //Free session data from this map server [Kevin]
  4883. unit_free_pc(sd);
  4884. return SETPOS_OK;
  4885. }
  4886. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4887. {
  4888. 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);
  4889. x = y = 0; // make it random
  4890. }
  4891. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4892. int c=0;
  4893. do {
  4894. x = rnd()%(map[m].xs-2)+1;
  4895. y = rnd()%(map[m].ys-2)+1;
  4896. c++;
  4897. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4898. 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));
  4899. return SETPOS_OK; //preventing warp
  4900. //break; //allow warp anyway
  4901. }
  4902. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4903. }
  4904. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4905. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4906. vending_closevending(sd);
  4907. }
  4908. if(sd->bl.prev != NULL){
  4909. unit_remove_map_pc(sd,clrtype);
  4910. clif_changemap(sd,m,x,y); // [MouseJstr]
  4911. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4912. sd->state.rewarp = 1;
  4913. sd->mapindex = mapindex;
  4914. sd->bl.m = m;
  4915. sd->bl.x = sd->ud.to_x = x;
  4916. sd->bl.y = sd->ud.to_y = y;
  4917. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4918. { // Increased guild castle regen [Valaris]
  4919. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4920. if(gc && gc->guild_id == sd->status.guild_id)
  4921. sd->regen.state.gc = 1;
  4922. }
  4923. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4924. {
  4925. sd->pd->bl.m = m;
  4926. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4927. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4928. sd->pd->ud.dir = sd->ud.dir;
  4929. }
  4930. if( hom_is_active(sd->hd) )
  4931. {
  4932. sd->hd->bl.m = m;
  4933. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4934. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4935. sd->hd->ud.dir = sd->ud.dir;
  4936. }
  4937. if( sd->md )
  4938. {
  4939. sd->md->bl.m = m;
  4940. sd->md->bl.x = sd->md->ud.to_x = x;
  4941. sd->md->bl.y = sd->md->ud.to_y = y;
  4942. sd->md->ud.dir = sd->ud.dir;
  4943. }
  4944. if( sd->ed ) {
  4945. sd->ed->bl.m = m;
  4946. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4947. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4948. sd->ed->ud.dir = sd->ud.dir;
  4949. }
  4950. pc_cell_basilica(sd);
  4951. //check if we gonna be rewarped [lighta]
  4952. if(npc_check_areanpc(1,m,x,y,0)){
  4953. sd->count_rewarp++;
  4954. }
  4955. else
  4956. sd->count_rewarp = 0;
  4957. return SETPOS_OK;
  4958. }
  4959. /*==========================================
  4960. * Warp player sd to random location on current map.
  4961. * May fail if no walkable cell found (1000 attempts).
  4962. * Return:
  4963. * 0 = Success
  4964. * 1,2,3 = Fail
  4965. *------------------------------------------*/
  4966. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4967. {
  4968. int x,y,i=0;
  4969. int16 m;
  4970. nullpo_ret(sd);
  4971. m=sd->bl.m;
  4972. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4973. return 3;
  4974. do {
  4975. x = rnd()%(map[m].xs-2)+1;
  4976. y = rnd()%(map[m].ys-2)+1;
  4977. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  4978. if (i < 1000)
  4979. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4980. return 3;
  4981. }
  4982. /*==========================================
  4983. * Records a memo point at sd's current position
  4984. * pos - entry to replace, (-1: shift oldest entry out)
  4985. *------------------------------------------*/
  4986. bool pc_memo(struct map_session_data* sd, int pos)
  4987. {
  4988. int skill;
  4989. nullpo_ret(sd);
  4990. // check mapflags
  4991. if( sd->bl.m >= 0 && (map[sd->bl.m].flag.nomemo || map[sd->bl.m].flag.nowarpto) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  4992. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4993. return false;
  4994. }
  4995. // check inputs
  4996. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4997. return false; // invalid input
  4998. // check required skill level
  4999. skill = pc_checkskill(sd, AL_WARP);
  5000. if( skill < 1 ) {
  5001. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5002. return false;
  5003. }
  5004. if( skill < 2 || skill - 2 < pos ) {
  5005. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5006. return false;
  5007. }
  5008. if( pos == -1 )
  5009. {
  5010. uint8 i;
  5011. // prevent memo-ing the same map multiple times
  5012. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5013. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5014. pos = 0;
  5015. }
  5016. if( map[sd->bl.m].instance_id ) {
  5017. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5018. return false;
  5019. }
  5020. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5021. sd->status.memo_point[pos].x = sd->bl.x;
  5022. sd->status.memo_point[pos].y = sd->bl.y;
  5023. clif_skill_memomessage(sd, 0);
  5024. return true;
  5025. }
  5026. //
  5027. // Skills
  5028. //
  5029. /**
  5030. * Get the skill current cooldown for player.
  5031. * (get the db base cooldown for skill + player specific cooldown)
  5032. * @param sd : player pointer
  5033. * @param id : skill id
  5034. * @param lv : skill lv
  5035. * @return player skill cooldown
  5036. */
  5037. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5038. uint8 i;
  5039. uint16 idx = skill_get_index(skill_id);
  5040. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5041. if (!idx) return 0;
  5042. if (skill_db[idx]->cooldown[skill_lv - 1])
  5043. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5044. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5045. if (i < cooldownlen) {
  5046. cooldown += sd->skillcooldown[i].val;
  5047. cooldown = max(0,cooldown);
  5048. }
  5049. return cooldown;
  5050. }
  5051. /*==========================================
  5052. * Return player sd skill_lv learned for given skill
  5053. *------------------------------------------*/
  5054. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5055. {
  5056. uint16 idx = 0;
  5057. if (sd == NULL)
  5058. return 0;
  5059. if ((idx = skill_get_index(skill_id)) == 0) {
  5060. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5061. return 0;
  5062. }
  5063. if (SKILL_CHK_GUILD(skill_id) ) {
  5064. struct guild *g;
  5065. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5066. return guild_checkskill(g,skill_id);
  5067. return 0;
  5068. }
  5069. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5070. }
  5071. /**
  5072. * Check if we still have the correct weapon to continue the skill (actually status)
  5073. * If not ending it
  5074. * @param sd
  5075. * @return 0:error, 1:check done
  5076. */
  5077. static void pc_checkallowskill(struct map_session_data *sd)
  5078. {
  5079. const enum sc_type scw_list[] = {
  5080. SC_TWOHANDQUICKEN,
  5081. SC_ONEHAND,
  5082. SC_AURABLADE,
  5083. SC_PARRYING,
  5084. SC_SPEARQUICKEN,
  5085. SC_ADRENALINE,
  5086. SC_ADRENALINE2,
  5087. SC_DANCING,
  5088. SC_GATLINGFEVER,
  5089. };
  5090. uint8 i;
  5091. nullpo_retv(sd);
  5092. if(!sd->sc.count)
  5093. return;
  5094. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5095. { // Skills requiring specific weapon types
  5096. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5097. continue;
  5098. if(sd->sc.data[scw_list[i]] &&
  5099. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5100. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5101. }
  5102. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5103. // Spurt requires bare hands (feet, in fact xD)
  5104. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5105. if(sd->status.shield <= 0) { // Skills requiring a shield
  5106. const enum sc_type scs_list[] = {
  5107. SC_AUTOGUARD,
  5108. SC_DEFENDER,
  5109. SC_REFLECTSHIELD,
  5110. SC_REFLECTDAMAGE
  5111. };
  5112. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5113. if(sd->sc.data[scs_list[i]])
  5114. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5115. }
  5116. }
  5117. /*==========================================
  5118. * Return equipped index of item on player sd at pos
  5119. * Return
  5120. * -1 : Nothing equipped
  5121. * idx : (this index could be used in inventory to found item_data)
  5122. *------------------------------------------*/
  5123. short pc_checkequip(struct map_session_data *sd,int pos)
  5124. {
  5125. uint8 i;
  5126. nullpo_retr(-1, sd);
  5127. for(i=0;i<EQI_MAX;i++){
  5128. if(pos & equip_bitmask[i])
  5129. return sd->equip_index[i];
  5130. }
  5131. return -1;
  5132. }
  5133. /*==========================================
  5134. * Check if sd has nameid equipped somewhere
  5135. * @sd : the player session
  5136. * @nameid : id of the item to check
  5137. * @min : : see pc.h enum equip_index from ? to @max
  5138. * @max : see pc.h enum equip_index for @min to ?
  5139. * -return true,false
  5140. *------------------------------------------*/
  5141. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5142. {
  5143. int i;
  5144. for(i = min; i < max; i++) {
  5145. if(equip_bitmask[i]) {
  5146. int idx = sd->equip_index[i];
  5147. if (sd->status.inventory[idx].nameid == nameid)
  5148. return true;
  5149. }
  5150. }
  5151. return false;
  5152. }
  5153. /*==========================================
  5154. * Convert's from the client's lame Job ID system
  5155. * to the map server's 'makes sense' system. [Skotlex]
  5156. *------------------------------------------*/
  5157. int pc_jobid2mapid(unsigned short b_class)
  5158. {
  5159. switch(b_class)
  5160. {
  5161. //Novice And 1-1 Jobs
  5162. case JOB_NOVICE: return MAPID_NOVICE;
  5163. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5164. case JOB_MAGE: return MAPID_MAGE;
  5165. case JOB_ARCHER: return MAPID_ARCHER;
  5166. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5167. case JOB_MERCHANT: return MAPID_MERCHANT;
  5168. case JOB_THIEF: return MAPID_THIEF;
  5169. case JOB_TAEKWON: return MAPID_TAEKWON;
  5170. case JOB_WEDDING: return MAPID_WEDDING;
  5171. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5172. case JOB_NINJA: return MAPID_NINJA;
  5173. case JOB_XMAS: return MAPID_XMAS;
  5174. case JOB_SUMMER: return MAPID_SUMMER;
  5175. case JOB_HANBOK: return MAPID_HANBOK;
  5176. case JOB_GANGSI: return MAPID_GANGSI;
  5177. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5178. //2-1 Jobs
  5179. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5180. case JOB_KNIGHT: return MAPID_KNIGHT;
  5181. case JOB_WIZARD: return MAPID_WIZARD;
  5182. case JOB_HUNTER: return MAPID_HUNTER;
  5183. case JOB_PRIEST: return MAPID_PRIEST;
  5184. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5185. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5186. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5187. case JOB_KAGEROU:
  5188. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5189. case JOB_REBELLION: return MAPID_REBELLION;
  5190. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5191. //2-2 Jobs
  5192. case JOB_CRUSADER: return MAPID_CRUSADER;
  5193. case JOB_SAGE: return MAPID_SAGE;
  5194. case JOB_BARD:
  5195. case JOB_DANCER: return MAPID_BARDDANCER;
  5196. case JOB_MONK: return MAPID_MONK;
  5197. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5198. case JOB_ROGUE: return MAPID_ROGUE;
  5199. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5200. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5201. //Trans Novice And Trans 1-1 Jobs
  5202. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5203. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5204. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5205. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5206. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5207. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5208. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5209. //Trans 2-1 Jobs
  5210. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5211. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5212. case JOB_SNIPER: return MAPID_SNIPER;
  5213. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5214. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5215. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5216. //Trans 2-2 Jobs
  5217. case JOB_PALADIN: return MAPID_PALADIN;
  5218. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5219. case JOB_CLOWN:
  5220. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5221. case JOB_CHAMPION: return MAPID_CHAMPION;
  5222. case JOB_CREATOR: return MAPID_CREATOR;
  5223. case JOB_STALKER: return MAPID_STALKER;
  5224. //Baby Novice And Baby 1-1 Jobs
  5225. case JOB_BABY: return MAPID_BABY;
  5226. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5227. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5228. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5229. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5230. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5231. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5232. //Baby 2-1 Jobs
  5233. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5234. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5235. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5236. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5237. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5238. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5239. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5240. //Baby 2-2 Jobs
  5241. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5242. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5243. case JOB_BABY_BARD:
  5244. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5245. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5246. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5247. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5248. //3-1 Jobs
  5249. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5250. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5251. case JOB_WARLOCK: return MAPID_WARLOCK;
  5252. case JOB_RANGER: return MAPID_RANGER;
  5253. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5254. case JOB_MECHANIC: return MAPID_MECHANIC;
  5255. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5256. //3-2 Jobs
  5257. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5258. case JOB_SORCERER: return MAPID_SORCERER;
  5259. case JOB_MINSTREL:
  5260. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5261. case JOB_SURA: return MAPID_SURA;
  5262. case JOB_GENETIC: return MAPID_GENETIC;
  5263. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5264. //Trans 3-1 Jobs
  5265. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5266. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5267. case JOB_RANGER_T: return MAPID_RANGER_T;
  5268. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5269. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5270. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5271. //Trans 3-2 Jobs
  5272. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5273. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5274. case JOB_MINSTREL_T:
  5275. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5276. case JOB_SURA_T: return MAPID_SURA_T;
  5277. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5278. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5279. //Baby 3-1 Jobs
  5280. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5281. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5282. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5283. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5284. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5285. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5286. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5287. //Baby 3-2 Jobs
  5288. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5289. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5290. case JOB_BABY_MINSTREL:
  5291. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5292. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5293. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5294. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5295. //Doram Jobs
  5296. case JOB_SUMMONER: return MAPID_SUMMONER;
  5297. default:
  5298. return -1;
  5299. }
  5300. }
  5301. //Reverts the map-style class id to the client-style one.
  5302. int pc_mapid2jobid(unsigned short class_, int sex)
  5303. {
  5304. switch(class_) {
  5305. //Novice And 1-1 Jobs
  5306. case MAPID_NOVICE: return JOB_NOVICE;
  5307. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5308. case MAPID_MAGE: return JOB_MAGE;
  5309. case MAPID_ARCHER: return JOB_ARCHER;
  5310. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5311. case MAPID_MERCHANT: return JOB_MERCHANT;
  5312. case MAPID_THIEF: return JOB_THIEF;
  5313. case MAPID_TAEKWON: return JOB_TAEKWON;
  5314. case MAPID_WEDDING: return JOB_WEDDING;
  5315. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5316. case MAPID_NINJA: return JOB_NINJA;
  5317. case MAPID_XMAS: return JOB_XMAS;
  5318. case MAPID_SUMMER: return JOB_SUMMER;
  5319. case MAPID_HANBOK: return JOB_HANBOK;
  5320. case MAPID_GANGSI: return JOB_GANGSI;
  5321. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5322. //2-1 Jobs
  5323. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5324. case MAPID_KNIGHT: return JOB_KNIGHT;
  5325. case MAPID_WIZARD: return JOB_WIZARD;
  5326. case MAPID_HUNTER: return JOB_HUNTER;
  5327. case MAPID_PRIEST: return JOB_PRIEST;
  5328. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5329. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5330. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5331. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5332. case MAPID_REBELLION: return JOB_REBELLION;
  5333. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5334. //2-2 Jobs
  5335. case MAPID_CRUSADER: return JOB_CRUSADER;
  5336. case MAPID_SAGE: return JOB_SAGE;
  5337. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5338. case MAPID_MONK: return JOB_MONK;
  5339. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5340. case MAPID_ROGUE: return JOB_ROGUE;
  5341. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5342. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5343. //Trans Novice And Trans 2-1 Jobs
  5344. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5345. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5346. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5347. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5348. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5349. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5350. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5351. //Trans 2-1 Jobs
  5352. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5353. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5354. case MAPID_SNIPER: return JOB_SNIPER;
  5355. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5356. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5357. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5358. //Trans 2-2 Jobs
  5359. case MAPID_PALADIN: return JOB_PALADIN;
  5360. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5361. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5362. case MAPID_CHAMPION: return JOB_CHAMPION;
  5363. case MAPID_CREATOR: return JOB_CREATOR;
  5364. case MAPID_STALKER: return JOB_STALKER;
  5365. //Baby Novice And Baby 1-1 Jobs
  5366. case MAPID_BABY: return JOB_BABY;
  5367. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5368. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5369. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5370. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5371. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5372. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5373. //Baby 2-1 Jobs
  5374. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5375. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5376. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5377. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5378. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5379. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5380. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5381. //Baby 2-2 Jobs
  5382. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5383. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5384. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5385. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5386. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5387. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5388. //3-1 Jobs
  5389. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5390. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5391. case MAPID_WARLOCK: return JOB_WARLOCK;
  5392. case MAPID_RANGER: return JOB_RANGER;
  5393. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5394. case MAPID_MECHANIC: return JOB_MECHANIC;
  5395. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5396. //3-2 Jobs
  5397. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5398. case MAPID_SORCERER: return JOB_SORCERER;
  5399. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5400. case MAPID_SURA: return JOB_SURA;
  5401. case MAPID_GENETIC: return JOB_GENETIC;
  5402. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5403. //Trans 3-1 Jobs
  5404. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5405. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5406. case MAPID_RANGER_T: return JOB_RANGER_T;
  5407. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5408. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5409. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5410. //Trans 3-2 Jobs
  5411. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5412. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5413. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5414. case MAPID_SURA_T: return JOB_SURA_T;
  5415. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5416. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5417. //Baby 3-1 Jobs
  5418. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5419. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5420. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5421. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5422. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5423. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5424. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5425. //Baby 3-2 Jobs
  5426. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5427. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5428. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5429. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5430. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5431. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5432. //Doram Jobs
  5433. case MAPID_SUMMONER: return JOB_SUMMONER;
  5434. default:
  5435. return -1;
  5436. }
  5437. }
  5438. /*====================================================
  5439. * This function return the name of the job (by [Yor])
  5440. *----------------------------------------------------*/
  5441. const char* job_name(int class_)
  5442. {
  5443. switch (class_) {
  5444. case JOB_NOVICE:
  5445. case JOB_SWORDMAN:
  5446. case JOB_MAGE:
  5447. case JOB_ARCHER:
  5448. case JOB_ACOLYTE:
  5449. case JOB_MERCHANT:
  5450. case JOB_THIEF:
  5451. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5452. case JOB_KNIGHT:
  5453. case JOB_PRIEST:
  5454. case JOB_WIZARD:
  5455. case JOB_BLACKSMITH:
  5456. case JOB_HUNTER:
  5457. case JOB_ASSASSIN:
  5458. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5459. case JOB_KNIGHT2:
  5460. return msg_txt(NULL,557);
  5461. case JOB_CRUSADER:
  5462. case JOB_MONK:
  5463. case JOB_SAGE:
  5464. case JOB_ROGUE:
  5465. case JOB_ALCHEMIST:
  5466. case JOB_BARD:
  5467. case JOB_DANCER:
  5468. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5469. case JOB_CRUSADER2:
  5470. return msg_txt(NULL,563);
  5471. case JOB_WEDDING:
  5472. case JOB_SUPER_NOVICE:
  5473. case JOB_GUNSLINGER:
  5474. case JOB_NINJA:
  5475. case JOB_XMAS:
  5476. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5477. case JOB_SUMMER:
  5478. return msg_txt(NULL,621);
  5479. case JOB_HANBOK:
  5480. return msg_txt(NULL,694);
  5481. case JOB_OKTOBERFEST:
  5482. return msg_txt(NULL,696);
  5483. case JOB_NOVICE_HIGH:
  5484. case JOB_SWORDMAN_HIGH:
  5485. case JOB_MAGE_HIGH:
  5486. case JOB_ARCHER_HIGH:
  5487. case JOB_ACOLYTE_HIGH:
  5488. case JOB_MERCHANT_HIGH:
  5489. case JOB_THIEF_HIGH:
  5490. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5491. case JOB_LORD_KNIGHT:
  5492. case JOB_HIGH_PRIEST:
  5493. case JOB_HIGH_WIZARD:
  5494. case JOB_WHITESMITH:
  5495. case JOB_SNIPER:
  5496. case JOB_ASSASSIN_CROSS:
  5497. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5498. case JOB_LORD_KNIGHT2:
  5499. return msg_txt(NULL,582);
  5500. case JOB_PALADIN:
  5501. case JOB_CHAMPION:
  5502. case JOB_PROFESSOR:
  5503. case JOB_STALKER:
  5504. case JOB_CREATOR:
  5505. case JOB_CLOWN:
  5506. case JOB_GYPSY:
  5507. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5508. case JOB_PALADIN2:
  5509. return msg_txt(NULL,588);
  5510. case JOB_BABY:
  5511. case JOB_BABY_SWORDMAN:
  5512. case JOB_BABY_MAGE:
  5513. case JOB_BABY_ARCHER:
  5514. case JOB_BABY_ACOLYTE:
  5515. case JOB_BABY_MERCHANT:
  5516. case JOB_BABY_THIEF:
  5517. return msg_txt(NULL,595 - JOB_BABY + class_);
  5518. case JOB_BABY_KNIGHT:
  5519. case JOB_BABY_PRIEST:
  5520. case JOB_BABY_WIZARD:
  5521. case JOB_BABY_BLACKSMITH:
  5522. case JOB_BABY_HUNTER:
  5523. case JOB_BABY_ASSASSIN:
  5524. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5525. case JOB_BABY_KNIGHT2:
  5526. return msg_txt(NULL,602);
  5527. case JOB_BABY_CRUSADER:
  5528. case JOB_BABY_MONK:
  5529. case JOB_BABY_SAGE:
  5530. case JOB_BABY_ROGUE:
  5531. case JOB_BABY_ALCHEMIST:
  5532. case JOB_BABY_BARD:
  5533. case JOB_BABY_DANCER:
  5534. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5535. case JOB_BABY_CRUSADER2:
  5536. return msg_txt(NULL,608);
  5537. case JOB_SUPER_BABY:
  5538. return msg_txt(NULL,615);
  5539. case JOB_TAEKWON:
  5540. return msg_txt(NULL,616);
  5541. case JOB_STAR_GLADIATOR:
  5542. case JOB_STAR_GLADIATOR2:
  5543. return msg_txt(NULL,617);
  5544. case JOB_SOUL_LINKER:
  5545. return msg_txt(NULL,618);
  5546. case JOB_GANGSI:
  5547. case JOB_DEATH_KNIGHT:
  5548. case JOB_DARK_COLLECTOR:
  5549. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5550. case JOB_RUNE_KNIGHT:
  5551. case JOB_WARLOCK:
  5552. case JOB_RANGER:
  5553. case JOB_ARCH_BISHOP:
  5554. case JOB_MECHANIC:
  5555. case JOB_GUILLOTINE_CROSS:
  5556. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5557. case JOB_RUNE_KNIGHT_T:
  5558. case JOB_WARLOCK_T:
  5559. case JOB_RANGER_T:
  5560. case JOB_ARCH_BISHOP_T:
  5561. case JOB_MECHANIC_T:
  5562. case JOB_GUILLOTINE_CROSS_T:
  5563. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5564. case JOB_ROYAL_GUARD:
  5565. case JOB_SORCERER:
  5566. case JOB_MINSTREL:
  5567. case JOB_WANDERER:
  5568. case JOB_SURA:
  5569. case JOB_GENETIC:
  5570. case JOB_SHADOW_CHASER:
  5571. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5572. case JOB_ROYAL_GUARD_T:
  5573. case JOB_SORCERER_T:
  5574. case JOB_MINSTREL_T:
  5575. case JOB_WANDERER_T:
  5576. case JOB_SURA_T:
  5577. case JOB_GENETIC_T:
  5578. case JOB_SHADOW_CHASER_T:
  5579. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5580. case JOB_RUNE_KNIGHT2:
  5581. case JOB_RUNE_KNIGHT_T2:
  5582. return msg_txt(NULL,625);
  5583. case JOB_ROYAL_GUARD2:
  5584. case JOB_ROYAL_GUARD_T2:
  5585. return msg_txt(NULL,631);
  5586. case JOB_RANGER2:
  5587. case JOB_RANGER_T2:
  5588. return msg_txt(NULL,627);
  5589. case JOB_MECHANIC2:
  5590. case JOB_MECHANIC_T2:
  5591. return msg_txt(NULL,629);
  5592. case JOB_BABY_RUNE:
  5593. case JOB_BABY_WARLOCK:
  5594. case JOB_BABY_RANGER:
  5595. case JOB_BABY_BISHOP:
  5596. case JOB_BABY_MECHANIC:
  5597. case JOB_BABY_CROSS:
  5598. case JOB_BABY_GUARD:
  5599. case JOB_BABY_SORCERER:
  5600. case JOB_BABY_MINSTREL:
  5601. case JOB_BABY_WANDERER:
  5602. case JOB_BABY_SURA:
  5603. case JOB_BABY_GENETIC:
  5604. case JOB_BABY_CHASER:
  5605. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5606. case JOB_BABY_RUNE2:
  5607. return msg_txt(NULL,638);
  5608. case JOB_BABY_GUARD2:
  5609. return msg_txt(NULL,644);
  5610. case JOB_BABY_RANGER2:
  5611. return msg_txt(NULL,640);
  5612. case JOB_BABY_MECHANIC2:
  5613. return msg_txt(NULL,642);
  5614. case JOB_SUPER_NOVICE_E:
  5615. case JOB_SUPER_BABY_E:
  5616. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5617. case JOB_KAGEROU:
  5618. case JOB_OBORO:
  5619. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5620. case JOB_REBELLION:
  5621. return msg_txt(NULL,695);
  5622. case JOB_SUMMONER:
  5623. return msg_txt(NULL,697);
  5624. default:
  5625. return msg_txt(NULL,655);
  5626. }
  5627. }
  5628. /*====================================================
  5629. * Timered function to make id follow a target.
  5630. * @id = bl.id (player only atm)
  5631. * target is define in sd->followtarget (bl.id)
  5632. * used by pc_follow
  5633. *----------------------------------------------------*/
  5634. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5635. {
  5636. struct map_session_data *sd;
  5637. struct block_list *tbl;
  5638. sd = map_id2sd(id);
  5639. nullpo_ret(sd);
  5640. if (sd->followtimer != tid){
  5641. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5642. sd->followtimer = INVALID_TIMER;
  5643. return 0;
  5644. }
  5645. sd->followtimer = INVALID_TIMER;
  5646. tbl = map_id2bl(sd->followtarget);
  5647. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5648. {
  5649. pc_stop_following(sd);
  5650. return 0;
  5651. }
  5652. // either player or target is currently detached from map blocks (could be teleporting),
  5653. // but still connected to this map, so we'll just increment the timer and check back later
  5654. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5655. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5656. {
  5657. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5658. if (!check_distance_bl(&sd->bl, tbl, 5))
  5659. unit_walktobl(&sd->bl, tbl, 5, 0);
  5660. } else
  5661. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5662. }
  5663. sd->followtimer = add_timer(
  5664. tick + 1000, // increase time a bit to loosen up map's load
  5665. pc_follow_timer, sd->bl.id, 0);
  5666. return 0;
  5667. }
  5668. int pc_stop_following (struct map_session_data *sd)
  5669. {
  5670. nullpo_ret(sd);
  5671. if (sd->followtimer != INVALID_TIMER) {
  5672. delete_timer(sd->followtimer,pc_follow_timer);
  5673. sd->followtimer = INVALID_TIMER;
  5674. }
  5675. sd->followtarget = -1;
  5676. sd->ud.target_to = 0;
  5677. unit_stop_walking(&sd->bl, 1);
  5678. return 0;
  5679. }
  5680. int pc_follow(struct map_session_data *sd,int target_id)
  5681. {
  5682. struct block_list *bl = map_id2bl(target_id);
  5683. if (bl == NULL /*|| bl->type != BL_PC*/)
  5684. return 1;
  5685. if (sd->followtimer != INVALID_TIMER)
  5686. pc_stop_following(sd);
  5687. sd->followtarget = target_id;
  5688. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5689. return 0;
  5690. }
  5691. int pc_checkbaselevelup(struct map_session_data *sd) {
  5692. unsigned int next = pc_nextbaseexp(sd);
  5693. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5694. return 0;
  5695. do {
  5696. sd->status.base_exp -= next;
  5697. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5698. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5699. sd->status.base_exp = next-1;
  5700. next = pc_gets_status_point(sd->status.base_level);
  5701. sd->status.base_level++;
  5702. sd->status.status_point += next;
  5703. if( pc_is_maxbaselv(sd) ){
  5704. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5705. break;
  5706. }
  5707. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5708. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5709. status_calc_pet(sd->pd,SCO_NONE);
  5710. clif_updatestatus(sd,SP_STATUSPOINT);
  5711. clif_updatestatus(sd,SP_BASELEVEL);
  5712. clif_updatestatus(sd,SP_BASEEXP);
  5713. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5714. status_calc_pc(sd,SCO_FORCE);
  5715. status_percent_heal(&sd->bl,100,100);
  5716. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5717. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5718. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5719. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5720. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5721. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5722. if (sd->state.snovice_dead_flag)
  5723. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5724. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5725. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5726. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5727. }
  5728. clif_misceffect(&sd->bl,0);
  5729. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5730. if(sd->status.party_id)
  5731. party_send_levelup(sd);
  5732. pc_baselevelchanged(sd);
  5733. return 1;
  5734. }
  5735. void pc_baselevelchanged(struct map_session_data *sd) {
  5736. uint8 i;
  5737. for( i = 0; i < EQI_MAX; i++ ) {
  5738. if( sd->equip_index[i] >= 0 ) {
  5739. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5740. pc_unequipitem(sd, sd->equip_index[i], 3);
  5741. }
  5742. }
  5743. pc_show_questinfo(sd);
  5744. }
  5745. int pc_checkjoblevelup(struct map_session_data *sd)
  5746. {
  5747. unsigned int next = pc_nextjobexp(sd);
  5748. nullpo_ret(sd);
  5749. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5750. return 0;
  5751. do {
  5752. sd->status.job_exp -= next;
  5753. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5754. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5755. sd->status.job_exp = next-1;
  5756. sd->status.job_level ++;
  5757. sd->status.skill_point ++;
  5758. if( pc_is_maxjoblv(sd) ){
  5759. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5760. break;
  5761. }
  5762. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5763. clif_updatestatus(sd,SP_JOBLEVEL);
  5764. clif_updatestatus(sd,SP_JOBEXP);
  5765. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5766. clif_updatestatus(sd,SP_SKILLPOINT);
  5767. status_calc_pc(sd,SCO_FORCE);
  5768. clif_misceffect(&sd->bl,1);
  5769. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5770. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5771. npc_script_event(sd, NPCE_JOBLVUP);
  5772. return 1;
  5773. }
  5774. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5775. * @param sd Player
  5776. * @param base_exp Base EXP before peronal bonuses
  5777. * @param job_exp Job EXP before peronal bonuses
  5778. * @param src Block list that affecting the exp calculation
  5779. */
  5780. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5781. {
  5782. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5783. if (src) {
  5784. struct status_data *status = status_get_status_data(src);
  5785. if( sd->expaddrace[status->race] )
  5786. bonus += sd->expaddrace[status->race];
  5787. if( sd->expaddrace[RC_ALL] )
  5788. bonus += sd->expaddrace[RC_ALL];
  5789. if( sd->expaddclass[status->class_] )
  5790. bonus += sd->expaddclass[status->class_];
  5791. if( sd->expaddclass[CLASS_ALL] )
  5792. bonus += sd->expaddclass[CLASS_ALL];
  5793. if (battle_config.pk_mode &&
  5794. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5795. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5796. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5797. vip_bonus_base = battle_config.vip_base_exp_increase;
  5798. vip_bonus_job = battle_config.vip_job_exp_increase;
  5799. }
  5800. }
  5801. // Give EXPBOOST for quests even if src is NULL.
  5802. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5803. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5804. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5805. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5806. }
  5807. if (*base_exp) {
  5808. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5809. *base_exp = cap_value(exp, 1, UINT_MAX);
  5810. }
  5811. // Give JEXPBOOST for quests even if src is NULL.
  5812. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5813. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5814. if (*job_exp) {
  5815. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5816. *job_exp = cap_value(exp, 1, UINT_MAX);
  5817. }
  5818. return;
  5819. }
  5820. /**
  5821. * Show EXP gained by player in percentage by @showexp
  5822. * @param sd Player
  5823. * @param base_exp Base EXP gained/loss
  5824. * @param next_base_exp Base EXP needed for next base level
  5825. * @param job_exp Job EXP gained/loss
  5826. * @param next_job_exp Job EXP needed for next job level
  5827. * @param lost True:EXP penalty, lose EXP
  5828. **/
  5829. 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) {
  5830. char output[CHAT_SIZE_MAX];
  5831. nullpo_retv(sd);
  5832. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5833. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5834. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5835. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5836. clif_disp_onlyself(sd, output, strlen(output));
  5837. }
  5838. /**
  5839. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5840. * @param sd Player
  5841. * @param src EXP source
  5842. * @param base_exp Base EXP gained
  5843. * @param base_exp Job EXP gained
  5844. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5845. * @return
  5846. **/
  5847. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5848. {
  5849. unsigned int nextb = 0, nextj = 0;
  5850. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5851. nullpo_retv(sd);
  5852. if(sd->bl.prev == NULL || pc_isdead(sd))
  5853. return;
  5854. if (!(exp_flag&2)) {
  5855. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5856. return; // no exp on pvp maps
  5857. if (sd->status.guild_id>0)
  5858. base_exp -= guild_payexp(sd,base_exp);
  5859. }
  5860. flag = ((base_exp) ? 1 : 0) |
  5861. ((job_exp) ? 2 : 0) |
  5862. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5863. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5864. if (!(exp_flag&2))
  5865. pc_calcexp(sd, &base_exp, &job_exp, src);
  5866. nextb = pc_nextbaseexp(sd);
  5867. nextj = pc_nextjobexp(sd);
  5868. if (flag&4){
  5869. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5870. base_exp = 0;
  5871. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5872. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5873. }
  5874. if (flag&8){
  5875. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5876. job_exp = 0;
  5877. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5878. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5879. }
  5880. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5881. //Note that this value should never be greater than the original
  5882. //therefore no overflow checks are needed. [Skotlex]
  5883. if (nextb > 0) {
  5884. float nextbp = (float) base_exp / (float) nextb;
  5885. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5886. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5887. }
  5888. if (nextj > 0) {
  5889. float nextjp = (float) job_exp / (float) nextj;
  5890. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5891. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5892. }
  5893. }
  5894. // Give EXP for Base Level
  5895. if (base_exp) {
  5896. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5897. sd->status.base_exp = UINT32_MAX;
  5898. else
  5899. sd->status.base_exp += base_exp;
  5900. if (!pc_checkbaselevelup(sd))
  5901. clif_updatestatus(sd,SP_BASEEXP);
  5902. }
  5903. // Give EXP for Job Level
  5904. if (job_exp) {
  5905. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5906. sd->status.job_exp = UINT32_MAX;
  5907. else
  5908. sd->status.job_exp += job_exp;
  5909. if (!pc_checkjoblevelup(sd))
  5910. clif_updatestatus(sd,SP_JOBEXP);
  5911. }
  5912. if (flag&1)
  5913. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5914. if (flag&2)
  5915. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5916. if (sd->state.showexp && (base_exp || job_exp))
  5917. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5918. }
  5919. /**
  5920. * Lost Base/Job EXP from a player
  5921. * @param sd Player
  5922. * @param base_exp Base EXP lost
  5923. * @param job_exp Job EXP lost
  5924. **/
  5925. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  5926. nullpo_retv(sd);
  5927. if (base_exp) {
  5928. base_exp = u32min(sd->status.base_exp, base_exp);
  5929. sd->status.base_exp -= base_exp;
  5930. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  5931. clif_updatestatus(sd, SP_BASEEXP);
  5932. }
  5933. if (job_exp) {
  5934. job_exp = u32min(sd->status.job_exp, job_exp);
  5935. sd->status.job_exp -= job_exp;
  5936. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  5937. clif_updatestatus(sd, SP_JOBEXP);
  5938. }
  5939. if (sd->state.showexp && (base_exp || job_exp))
  5940. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  5941. }
  5942. /**
  5943. * Returns max base level for this character's class.
  5944. * @param class_: Player's class
  5945. * @return Max Base Level
  5946. */
  5947. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  5948. return job_info[pc_class2idx(class_)].max_level[0];
  5949. }
  5950. /**
  5951. * Returns max base level for this character.
  5952. * @param sd Player
  5953. * @return Max Base Level
  5954. **/
  5955. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5956. return pc_class_maxbaselv(sd->status.class_);
  5957. }
  5958. /**
  5959. * Returns max job level for this character's class.
  5960. * @param class_: Player's class
  5961. * @return Max Job Level
  5962. */
  5963. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  5964. return job_info[pc_class2idx(class_)].max_level[1];
  5965. }
  5966. /**
  5967. * Returns max job level for this character.
  5968. * @param sd Player
  5969. * @return Max Job Level
  5970. **/
  5971. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5972. return pc_class_maxjoblv(sd->status.class_);
  5973. }
  5974. /**
  5975. * Check if player is reached max base level
  5976. * @param sd
  5977. * @return True if reached max level
  5978. **/
  5979. bool pc_is_maxbaselv(struct map_session_data *sd) {
  5980. nullpo_retr(false, sd);
  5981. return (sd->status.base_level >= pc_maxbaselv(sd));
  5982. }
  5983. /**
  5984. * Check if player is reached max base level
  5985. * @param sd
  5986. * @return True if reached max level
  5987. **/
  5988. bool pc_is_maxjoblv(struct map_session_data *sd) {
  5989. nullpo_retr(false, sd);
  5990. return (sd->status.job_level >= pc_maxjoblv(sd));
  5991. }
  5992. /**
  5993. * Base exp needed for player to level up.
  5994. * @param sd
  5995. * @return Base EXP needed for next base level
  5996. **/
  5997. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  5998. nullpo_ret(sd);
  5999. if (sd->status.base_level == 0) // Is this something that possible?
  6000. return 0;
  6001. if (pc_is_maxbaselv(sd))
  6002. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6003. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6004. }
  6005. /**
  6006. * Job exp needed for player to level up.
  6007. * @param sd
  6008. * @return Job EXP needed for next job level
  6009. **/
  6010. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6011. nullpo_ret(sd);
  6012. if (sd->status.job_level == 0) // Is this something that possible?
  6013. return 0;
  6014. if (pc_is_maxjoblv(sd))
  6015. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6016. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6017. }
  6018. /// Returns the value of the specified stat.
  6019. static int pc_getstat(struct map_session_data* sd, int type)
  6020. {
  6021. nullpo_retr(-1, sd);
  6022. switch( type ) {
  6023. case SP_STR: return sd->status.str;
  6024. case SP_AGI: return sd->status.agi;
  6025. case SP_VIT: return sd->status.vit;
  6026. case SP_INT: return sd->status.int_;
  6027. case SP_DEX: return sd->status.dex;
  6028. case SP_LUK: return sd->status.luk;
  6029. default:
  6030. return -1;
  6031. }
  6032. }
  6033. /// Sets the specified stat to the specified value.
  6034. /// Returns the new value.
  6035. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6036. {
  6037. nullpo_retr(-1, sd);
  6038. switch( type ) {
  6039. case SP_STR: sd->status.str = val; break;
  6040. case SP_AGI: sd->status.agi = val; break;
  6041. case SP_VIT: sd->status.vit = val; break;
  6042. case SP_INT: sd->status.int_ = val; break;
  6043. case SP_DEX: sd->status.dex = val; break;
  6044. case SP_LUK: sd->status.luk = val; break;
  6045. default:
  6046. return -1;
  6047. }
  6048. return val;
  6049. }
  6050. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6051. int pc_gets_status_point(int level)
  6052. {
  6053. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6054. return (statp[level+1] - statp[level]);
  6055. else //Default increase
  6056. return ((level+15) / 5);
  6057. }
  6058. #ifdef RENEWAL_STAT
  6059. /// Renewal status point cost formula
  6060. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6061. #else
  6062. /// Pre-Renewal status point cost formula
  6063. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6064. #endif
  6065. /// Returns the number of stat points needed to change the specified stat by val.
  6066. /// If val is negative, returns the number of stat points that would be needed to
  6067. /// raise the specified stat from (current value - val) to current value.
  6068. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6069. {
  6070. int low, high, sp = 0, max = 0;
  6071. if ( val == 0 )
  6072. return 0;
  6073. low = pc_getstat(sd,type);
  6074. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6075. if ( low >= max && val > 0 )
  6076. return 0; // Official servers show '0' when max is reached
  6077. high = low + val;
  6078. if ( val < 0 )
  6079. swap(low, high);
  6080. for ( ; low < high; low++ )
  6081. sp += PC_STATUS_POINT_COST(low);
  6082. return sp;
  6083. }
  6084. /**
  6085. * Returns the value the specified stat can be increased by with the current
  6086. * amount of available status points for the current character's class.
  6087. *
  6088. * @param sd The target character.
  6089. * @param type Stat to verify.
  6090. * @return Maximum value the stat could grow by.
  6091. */
  6092. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6093. {
  6094. int base, final_val, status_points, max_param;
  6095. nullpo_ret(sd);
  6096. base = final_val = pc_getstat(sd, type);
  6097. status_points = sd->status.status_point;
  6098. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6099. while (final_val <= max_param && status_points >= 0) {
  6100. status_points -= PC_STATUS_POINT_COST(final_val);
  6101. final_val++;
  6102. }
  6103. final_val--;
  6104. return (final_val > base ? final_val-base : 0);
  6105. }
  6106. /**
  6107. * Raises a stat by the specified amount.
  6108. *
  6109. * Obeys max_parameter limits.
  6110. * Subtracts status points according to the cost of the increased stat points.
  6111. *
  6112. * @param sd The target character.
  6113. * @param type The stat to change (see enum _sp)
  6114. * @param increase The stat increase (strictly positive) amount.
  6115. * @retval true if the stat was increased by any amount.
  6116. * @retval false if there were no changes.
  6117. */
  6118. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6119. {
  6120. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6121. nullpo_ret(sd);
  6122. // check conditions
  6123. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6124. clif_statusupack(sd, type, 0, 0);
  6125. return false;
  6126. }
  6127. // check limits
  6128. current = pc_getstat(sd, type);
  6129. max_increase = pc_maxparameterincrease(sd, type);
  6130. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6131. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6132. clif_statusupack(sd, type, 0, 0);
  6133. return false;
  6134. }
  6135. // check status points
  6136. needed_points = pc_need_status_point(sd, type, increase);
  6137. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6138. clif_statusupack(sd, type, 0, 0);
  6139. return false;
  6140. }
  6141. // set new values
  6142. final_value = pc_setstat(sd, type, current + increase);
  6143. sd->status.status_point -= needed_points;
  6144. status_calc_pc(sd,SCO_NONE);
  6145. // update increase cost indicator
  6146. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6147. // update statpoint count
  6148. clif_updatestatus(sd, SP_STATUSPOINT);
  6149. // update stat value
  6150. clif_statusupack(sd, type, 1, final_value); // required
  6151. if( final_value > 255 )
  6152. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6153. return true;
  6154. }
  6155. /**
  6156. * Raises a stat by the specified amount.
  6157. *
  6158. * Obeys max_parameter limits.
  6159. * Does not subtract status points for the cost of the modified stat points.
  6160. *
  6161. * @param sd The target character.
  6162. * @param type The stat to change (see enum _sp)
  6163. * @param val The stat increase (or decrease) amount.
  6164. * @return the stat increase amount.
  6165. * @retval 0 if no changes were made.
  6166. */
  6167. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6168. {
  6169. int max, need;
  6170. nullpo_ret(sd);
  6171. if( type < SP_STR || type > SP_LUK )
  6172. {
  6173. clif_statusupack(sd,type,0,0);
  6174. return 0;
  6175. }
  6176. need = pc_need_status_point(sd,type,1);
  6177. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6178. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6179. status_calc_pc(sd,SCO_NONE);
  6180. // update increase cost indicator
  6181. if( need != pc_need_status_point(sd,type,1) )
  6182. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6183. // update stat value
  6184. clif_statusupack(sd,type,1,val); // required
  6185. if( val > 255 )
  6186. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6187. return val;
  6188. }
  6189. /*==========================================
  6190. * Update skill_lv for player sd
  6191. * Skill point allocation
  6192. *------------------------------------------*/
  6193. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6194. {
  6195. uint16 idx = skill_get_index(skill_id);
  6196. nullpo_retv(sd);
  6197. if (!idx) {
  6198. if (skill_id)
  6199. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6200. return;
  6201. }
  6202. // Level up guild skill
  6203. if (SKILL_CHK_GUILD(skill_id)) {
  6204. guild_skillup(sd, skill_id);
  6205. return;
  6206. }
  6207. // Level up homunculus skill
  6208. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6209. hom_skillup(sd->hd, skill_id);
  6210. return;
  6211. }
  6212. else {
  6213. if( sd->status.skill_point > 0 &&
  6214. sd->status.skill[idx].id &&
  6215. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6216. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6217. {
  6218. int lv, range, upgradable;
  6219. sd->status.skill[idx].lv++;
  6220. sd->status.skill_point--;
  6221. if( !skill_get_inf(skill_id) )
  6222. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6223. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6224. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6225. else
  6226. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6227. lv = sd->status.skill[idx].lv;
  6228. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6229. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6230. clif_skillup(sd,skill_id,lv,range,upgradable);
  6231. clif_updatestatus(sd,SP_SKILLPOINT);
  6232. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6233. clif_updatestatus(sd,SP_CARTINFO);
  6234. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6235. clif_skillinfoblock(sd);
  6236. }
  6237. //else
  6238. // 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);
  6239. }
  6240. }
  6241. /*==========================================
  6242. * /allskill
  6243. *------------------------------------------*/
  6244. int pc_allskillup(struct map_session_data *sd)
  6245. {
  6246. int i;
  6247. nullpo_ret(sd);
  6248. for (i = 0; i < MAX_SKILL; i++) {
  6249. 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) {
  6250. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6251. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6252. if (sd->status.skill[i].lv == 0)
  6253. sd->status.skill[i].id = 0;
  6254. }
  6255. }
  6256. if (!pc_grant_allskills(sd, true)) {
  6257. uint16 sk_id;
  6258. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6259. int inf2 = 0;
  6260. uint16 sk_idx = 0;
  6261. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6262. continue;
  6263. inf2 = skill_get_inf2(sk_id);
  6264. if (
  6265. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6266. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6267. sk_id == SG_DEVIL
  6268. )
  6269. continue; //Cannot be learned normally.
  6270. sd->status.skill[sk_idx].id = sk_id;
  6271. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6272. }
  6273. }
  6274. status_calc_pc(sd,SCO_NONE);
  6275. //Required because if you could level up all skills previously,
  6276. //the update will not be sent as only the lv variable changes.
  6277. clif_skillinfoblock(sd);
  6278. return 0;
  6279. }
  6280. /*==========================================
  6281. * /resetlvl
  6282. *------------------------------------------*/
  6283. int pc_resetlvl(struct map_session_data* sd,int type)
  6284. {
  6285. int i;
  6286. nullpo_ret(sd);
  6287. if (type != 3) //Also reset skills
  6288. pc_resetskill(sd, 0);
  6289. if(type == 1){
  6290. sd->status.skill_point=0;
  6291. sd->status.base_level=1;
  6292. sd->status.job_level=1;
  6293. sd->status.base_exp=0;
  6294. sd->status.job_exp=0;
  6295. if(sd->sc.option !=0)
  6296. sd->sc.option = 0;
  6297. sd->status.str=1;
  6298. sd->status.agi=1;
  6299. sd->status.vit=1;
  6300. sd->status.int_=1;
  6301. sd->status.dex=1;
  6302. sd->status.luk=1;
  6303. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6304. sd->status.status_point=100; // not 88 [celest]
  6305. // give platinum skills upon changing
  6306. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6307. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6308. }
  6309. }
  6310. if(type == 2){
  6311. sd->status.skill_point=0;
  6312. sd->status.base_level=1;
  6313. sd->status.job_level=1;
  6314. sd->status.base_exp=0;
  6315. sd->status.job_exp=0;
  6316. }
  6317. if(type == 3){
  6318. sd->status.base_level=1;
  6319. sd->status.base_exp=0;
  6320. }
  6321. if(type == 4){
  6322. sd->status.job_level=1;
  6323. sd->status.job_exp=0;
  6324. }
  6325. clif_updatestatus(sd,SP_STATUSPOINT);
  6326. clif_updatestatus(sd,SP_STR);
  6327. clif_updatestatus(sd,SP_AGI);
  6328. clif_updatestatus(sd,SP_VIT);
  6329. clif_updatestatus(sd,SP_INT);
  6330. clif_updatestatus(sd,SP_DEX);
  6331. clif_updatestatus(sd,SP_LUK);
  6332. clif_updatestatus(sd,SP_BASELEVEL);
  6333. clif_updatestatus(sd,SP_JOBLEVEL);
  6334. clif_updatestatus(sd,SP_STATUSPOINT);
  6335. clif_updatestatus(sd,SP_BASEEXP);
  6336. clif_updatestatus(sd,SP_JOBEXP);
  6337. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6338. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6339. clif_updatestatus(sd,SP_SKILLPOINT);
  6340. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6341. clif_updatestatus(sd,SP_UAGI);
  6342. clif_updatestatus(sd,SP_UVIT);
  6343. clif_updatestatus(sd,SP_UINT);
  6344. clif_updatestatus(sd,SP_UDEX);
  6345. clif_updatestatus(sd,SP_ULUK); // End Addition
  6346. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6347. if(sd->equip_index[i] >= 0)
  6348. if(pc_isequip(sd,sd->equip_index[i]))
  6349. pc_unequipitem(sd,sd->equip_index[i],2);
  6350. }
  6351. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6352. party_send_levelup(sd);
  6353. status_calc_pc(sd, SCO_FORCE);
  6354. clif_skillinfoblock(sd);
  6355. return 0;
  6356. }
  6357. /*==========================================
  6358. * /resetstate
  6359. *------------------------------------------*/
  6360. int pc_resetstate(struct map_session_data* sd)
  6361. {
  6362. nullpo_ret(sd);
  6363. if (battle_config.use_statpoint_table)
  6364. { // New statpoint table used here - Dexity
  6365. if (sd->status.base_level > MAX_LEVEL)
  6366. { //statp[] goes out of bounds, can't reset!
  6367. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6368. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6369. return 0;
  6370. }
  6371. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6372. }
  6373. else
  6374. {
  6375. int add=0;
  6376. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6377. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6378. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6379. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6380. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6381. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6382. sd->status.status_point+=add;
  6383. }
  6384. pc_setstat(sd, SP_STR, 1);
  6385. pc_setstat(sd, SP_AGI, 1);
  6386. pc_setstat(sd, SP_VIT, 1);
  6387. pc_setstat(sd, SP_INT, 1);
  6388. pc_setstat(sd, SP_DEX, 1);
  6389. pc_setstat(sd, SP_LUK, 1);
  6390. clif_updatestatus(sd,SP_STR);
  6391. clif_updatestatus(sd,SP_AGI);
  6392. clif_updatestatus(sd,SP_VIT);
  6393. clif_updatestatus(sd,SP_INT);
  6394. clif_updatestatus(sd,SP_DEX);
  6395. clif_updatestatus(sd,SP_LUK);
  6396. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6397. clif_updatestatus(sd,SP_UAGI);
  6398. clif_updatestatus(sd,SP_UVIT);
  6399. clif_updatestatus(sd,SP_UINT);
  6400. clif_updatestatus(sd,SP_UDEX);
  6401. clif_updatestatus(sd,SP_ULUK); // End Addition
  6402. clif_updatestatus(sd,SP_STATUSPOINT);
  6403. if( sd->mission_mobid ) { //bugreport:2200
  6404. sd->mission_mobid = 0;
  6405. sd->mission_count = 0;
  6406. pc_setglobalreg(sd, add_str("TK_MISSION_ID"), 0);
  6407. }
  6408. status_calc_pc(sd, SCO_NONE);
  6409. return 1;
  6410. }
  6411. /*==========================================
  6412. * /resetskill
  6413. * if flag&1, perform block resync and status_calc call.
  6414. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6415. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6416. *------------------------------------------*/
  6417. int pc_resetskill(struct map_session_data* sd, int flag)
  6418. {
  6419. int i, skill_point=0;
  6420. nullpo_ret(sd);
  6421. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6422. return 0;
  6423. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6424. /**
  6425. * 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)
  6426. **/
  6427. if( pc_is_taekwon_ranker(sd) )
  6428. return 0;
  6429. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6430. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6431. i = sd->sc.option;
  6432. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6433. i &= ~OPTION_RIDING;
  6434. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6435. i &= ~OPTION_FALCON;
  6436. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6437. i &= ~OPTION_DRAGON;
  6438. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6439. i &= ~OPTION_WUG;
  6440. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6441. i &= ~OPTION_WUGRIDER;
  6442. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6443. i &= ~OPTION_MADOGEAR;
  6444. #ifndef NEW_CARTS
  6445. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6446. i &= ~OPTION_CART;
  6447. #else
  6448. if( sd->sc.data[SC_PUSH_CART] )
  6449. pc_setcart(sd, 0);
  6450. #endif
  6451. if( i != sd->sc.option )
  6452. pc_setoption(sd, i);
  6453. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6454. hom_vaporize(sd, HOM_ST_ACTIVE);
  6455. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6456. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6457. }
  6458. for( i = 1; i < MAX_SKILL; i++ )
  6459. {
  6460. uint8 lv = sd->status.skill[i].lv;
  6461. int inf2;
  6462. uint16 skill_id = skill_idx2id(i);
  6463. if (lv == 0 || skill_id == 0)
  6464. continue;
  6465. inf2 = skill_get_inf2(skill_id);
  6466. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6467. continue;
  6468. // Don't reset trick dead if not a novice/baby
  6469. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6470. {
  6471. sd->status.skill[i].lv = 0;
  6472. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6473. continue;
  6474. }
  6475. // do not reset basic skill
  6476. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6477. continue;
  6478. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6479. continue;
  6480. if( flag&4 && !skill_ischangesex(skill_id) )
  6481. continue;
  6482. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6483. { //Only handle quest skills in a special way when you can't learn them manually
  6484. if( battle_config.quest_skill_reset && !(flag&2) )
  6485. { //Wipe them
  6486. sd->status.skill[i].lv = 0;
  6487. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6488. }
  6489. continue;
  6490. }
  6491. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6492. skill_point += lv;
  6493. else
  6494. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6495. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6496. if( !(flag&2) )
  6497. {// reset
  6498. sd->status.skill[i].lv = 0;
  6499. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6500. }
  6501. }
  6502. if( flag&2 || !skill_point ) return skill_point;
  6503. sd->status.skill_point += skill_point;
  6504. if (flag&1) {
  6505. clif_updatestatus(sd,SP_SKILLPOINT);
  6506. clif_skillinfoblock(sd);
  6507. status_calc_pc(sd, SCO_FORCE);
  6508. }
  6509. return skill_point;
  6510. }
  6511. /*==========================================
  6512. * /resetfeel [Komurka]
  6513. *------------------------------------------*/
  6514. int pc_resetfeel(struct map_session_data* sd)
  6515. {
  6516. int i;
  6517. nullpo_ret(sd);
  6518. for (i=0; i<MAX_PC_FEELHATE; i++)
  6519. {
  6520. sd->feel_map[i].m = -1;
  6521. sd->feel_map[i].index = 0;
  6522. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6523. }
  6524. return 0;
  6525. }
  6526. int pc_resethate(struct map_session_data* sd)
  6527. {
  6528. int i;
  6529. nullpo_ret(sd);
  6530. for (i=0; i<3; i++)
  6531. {
  6532. sd->hate_mob[i] = -1;
  6533. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6534. }
  6535. return 0;
  6536. }
  6537. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6538. {
  6539. int i, bonus = 0;
  6540. nullpo_ret(sd);
  6541. skill_id = skill_dummy2skill_id(skill_id);
  6542. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6543. if( i < ARRAYLENGTH(sd->skillatk) )
  6544. bonus = sd->skillatk[i].val;
  6545. return bonus;
  6546. }
  6547. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6548. {
  6549. int i, bonus = 0;
  6550. nullpo_ret(sd);
  6551. skill_id = skill_dummy2skill_id(skill_id);
  6552. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6553. if( i < ARRAYLENGTH(sd->subskill) )
  6554. bonus = sd->subskill[i].val;
  6555. return bonus;
  6556. }
  6557. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6558. int i, bonus = sd->bonus.add_heal_rate;
  6559. skill_id = skill_dummy2skill_id(skill_id);
  6560. if( bonus ) {
  6561. switch( skill_id ) {
  6562. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6563. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6564. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6565. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6566. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6567. }
  6568. }
  6569. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6570. if( i < ARRAYLENGTH(sd->skillheal) )
  6571. bonus += sd->skillheal[i].val;
  6572. return bonus;
  6573. }
  6574. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6575. int i, bonus = sd->bonus.add_heal2_rate;
  6576. skill_id = skill_dummy2skill_id(skill_id);
  6577. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6578. if( i < ARRAYLENGTH(sd->skillheal2) )
  6579. bonus += sd->skillheal2[i].val;
  6580. return bonus;
  6581. }
  6582. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6583. {
  6584. if( !pc_isdead(sd) )
  6585. return; // not applicable
  6586. if( sd->bg_id && bg_member_respawn(sd) )
  6587. return; // member revived by battleground
  6588. pc_setstand(sd, true);
  6589. pc_setrestartvalue(sd,3);
  6590. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6591. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6592. }
  6593. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6594. {
  6595. struct map_session_data *sd = map_id2sd(id);
  6596. if( sd != NULL )
  6597. {
  6598. sd->pvp_point=0;
  6599. pc_respawn(sd,CLR_OUTSIGHT);
  6600. }
  6601. return 0;
  6602. }
  6603. /*==========================================
  6604. * Invoked when a player has received damage
  6605. *------------------------------------------*/
  6606. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6607. {
  6608. if (sp) clif_updatestatus(sd,SP_SP);
  6609. if (hp) clif_updatestatus(sd,SP_HP);
  6610. else return;
  6611. if (!src)
  6612. return;
  6613. if( pc_issit(sd) ) {
  6614. pc_setstand(sd, true);
  6615. skill_sit(sd,0);
  6616. }
  6617. if (sd->progressbar.npc_id)
  6618. clif_progressbar_abort(sd);
  6619. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6620. pet_target_check(sd->pd,src,1);
  6621. if( sd->status.ele_id > 0 )
  6622. elemental_set_target(sd,src);
  6623. sd->canlog_tick = gettick();
  6624. }
  6625. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6626. {
  6627. TBL_PC *sd = map_id2sd(id);
  6628. if(sd) pc_close_npc(sd,data);
  6629. return 0;
  6630. }
  6631. /*
  6632. * Method to properly close npc for player and clear anything related
  6633. * @flag == 1 : produce close button
  6634. * @flag == 2 : directly close it
  6635. */
  6636. void pc_close_npc(struct map_session_data *sd,int flag)
  6637. {
  6638. nullpo_retv(sd);
  6639. if (sd->npc_id || sd->npc_shopid) {
  6640. if (sd->state.using_fake_npc) {
  6641. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6642. sd->state.using_fake_npc = 0;
  6643. }
  6644. if (sd->st) {
  6645. if(sd->st->state == RUN){ //wait ending code execution
  6646. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6647. return;
  6648. }
  6649. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6650. sd->st->mes_active = 0;
  6651. }
  6652. sd->state.menu_or_input = 0;
  6653. sd->npc_menu = 0;
  6654. sd->npc_shopid = 0;
  6655. #ifdef SECURE_NPCTIMEOUT
  6656. sd->npc_idle_timer = INVALID_TIMER;
  6657. #endif
  6658. clif_scriptclose(sd,sd->npc_id);
  6659. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6660. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6661. script_free_state(sd->st);
  6662. sd->st = NULL;
  6663. sd->npc_id = 0;
  6664. }
  6665. }
  6666. }
  6667. /*==========================================
  6668. * Invoked when a player has negative current hp
  6669. *------------------------------------------*/
  6670. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6671. {
  6672. int i=0,k=0;
  6673. unsigned int tick = gettick();
  6674. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6675. // Super Novices have no kill or die functions attached when saved by their angel
  6676. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6677. unsigned int exp = pc_nextbaseexp(sd);
  6678. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6679. sd->state.snovice_dead_flag = 1;
  6680. pc_setrestartvalue(sd,1);
  6681. status_percent_heal(&sd->bl, 100, 100);
  6682. clif_resurrection(&sd->bl, 1);
  6683. if(battle_config.pc_invincible_time)
  6684. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6685. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6686. if(map_flag_gvg(sd->bl.m))
  6687. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6688. return 0;
  6689. }
  6690. }
  6691. for(k = 0; k < MAX_DEVOTION; k++) {
  6692. if (sd->devotion[k]){
  6693. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6694. if (devsd)
  6695. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6696. sd->devotion[k] = 0;
  6697. }
  6698. }
  6699. if(sd->shadowform_id) { //if we were target of shadowform
  6700. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6701. sd->shadowform_id = 0; //should be remove on status end anyway
  6702. }
  6703. if(sd->status.pet_id > 0 && sd->pd) {
  6704. struct pet_data *pd = sd->pd;
  6705. if( !map[sd->bl.m].flag.noexppenalty ) {
  6706. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6707. if( pd->pet.intimate < 0 )
  6708. pd->pet.intimate = 0;
  6709. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6710. }
  6711. if( sd->pd->target_id ) // Unlock all targets...
  6712. pet_unlocktarget(sd->pd);
  6713. }
  6714. if (sd->status.hom_id > 0) {
  6715. if(battle_config.homunculus_auto_vapor && sd->hd)
  6716. hom_vaporize(sd, HOM_ST_ACTIVE);
  6717. }
  6718. if( sd->md )
  6719. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6720. if( sd->ed )
  6721. elemental_delete(sd->ed);
  6722. // Leave duel if you die [LuzZza]
  6723. if(battle_config.duel_autoleave_when_die) {
  6724. if(sd->duel_group > 0)
  6725. duel_leave(sd->duel_group, sd);
  6726. if(sd->duel_invite > 0)
  6727. duel_reject(sd->duel_invite, sd);
  6728. }
  6729. pc_close_npc(sd,2); //close npc if we were using one
  6730. /* e.g. not killed thru pc_damage */
  6731. if( pc_issit(sd) ) {
  6732. clif_status_load(&sd->bl,SI_SIT,0);
  6733. }
  6734. pc_setdead(sd);
  6735. pc_setglobalreg(sd, add_str("PC_DIE_COUNTER"), sd->die_counter+1);
  6736. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6737. //Reset menu skills/item skills
  6738. if ((sd->skillitem) != 0)
  6739. sd->skillitem = sd->skillitemlv = 0;
  6740. if ((sd->menuskill_id) != 0)
  6741. sd->menuskill_id = sd->menuskill_val = 0;
  6742. //Reset ticks.
  6743. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6744. if ( sd->spiritball !=0 )
  6745. pc_delspiritball(sd,sd->spiritball,0);
  6746. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6747. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6748. if (src)
  6749. switch (src->type) {
  6750. case BL_MOB:
  6751. {
  6752. struct mob_data *md=(struct mob_data *)src;
  6753. if(md->target_id==sd->bl.id)
  6754. mob_unlocktarget(md,tick);
  6755. if(battle_config.mobs_level_up && md->status.hp &&
  6756. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6757. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6758. ) { // monster level up [Valaris]
  6759. clif_misceffect(&md->bl,0);
  6760. md->level++;
  6761. status_calc_mob(md, SCO_NONE);
  6762. status_percent_heal(src,10,0);
  6763. if( battle_config.show_mob_info&4 )
  6764. {// update name with new level
  6765. clif_name_area(&md->bl);
  6766. }
  6767. }
  6768. src = battle_get_master(src); // Maybe Player Summon
  6769. }
  6770. break;
  6771. case BL_PET: //Pass on to master...
  6772. case BL_HOM:
  6773. case BL_MER:
  6774. src = battle_get_master(src);
  6775. break;
  6776. }
  6777. if (src && src->type == BL_PC) {
  6778. struct map_session_data *ssd = (struct map_session_data *)src;
  6779. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6780. npc_script_event(ssd, NPCE_KILLPC);
  6781. if (battle_config.pk_mode&2) {
  6782. ssd->status.manner -= 5;
  6783. if(ssd->status.manner < 0)
  6784. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6785. #if 0
  6786. // PK/Karma system code (not enabled yet) [celest]
  6787. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6788. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6789. // karma going down = more 'good' / more honourable.
  6790. // The Karma System way...
  6791. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6792. sd->status.karma--;
  6793. ssd->status.karma--;
  6794. }
  6795. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6796. ssd->status.karma++;
  6797. // or the PK System way...
  6798. if (sd->status.karma > 0) // player killed is dishonourable?
  6799. ssd->status.karma--; // honour points earned
  6800. sd->status.karma++; // honour points lost
  6801. // To-do: Receive exp on certain occasions
  6802. #endif
  6803. }
  6804. }
  6805. if(battle_config.bone_drop==2
  6806. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6807. {
  6808. struct item item_tmp;
  6809. memset(&item_tmp,0,sizeof(item_tmp));
  6810. item_tmp.nameid=ITEMID_SKULL_;
  6811. item_tmp.identify=1;
  6812. item_tmp.card[0]=CARD0_CREATE;
  6813. item_tmp.card[1]=0;
  6814. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6815. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6816. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6817. }
  6818. //Remove bonus_script when dead
  6819. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6820. // changed penalty options, added death by player if pk_mode [Valaris]
  6821. if(battle_config.death_penalty_type
  6822. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6823. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6824. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6825. {
  6826. uint32 base_penalty = 0;
  6827. uint32 job_penalty = 0;
  6828. uint32 zeny_penalty = 0;
  6829. if (pc_isvip(sd)) { // EXP penalty for VIP
  6830. base_penalty = battle_config.vip_exp_penalty_base;
  6831. job_penalty = battle_config.vip_exp_penalty_job;
  6832. zeny_penalty = battle_config.vip_zeny_penalty;
  6833. } else {
  6834. base_penalty = battle_config.death_penalty_base;
  6835. job_penalty = battle_config.death_penalty_job;
  6836. zeny_penalty = battle_config.zeny_penalty;
  6837. }
  6838. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6839. switch (battle_config.death_penalty_type) {
  6840. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6841. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6842. }
  6843. if (base_penalty){ //recheck after altering to speedup
  6844. if (battle_config.pk_mode && src && src->type==BL_PC)
  6845. base_penalty *= 2;
  6846. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6847. }
  6848. }
  6849. else
  6850. base_penalty = 0;
  6851. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6852. switch (battle_config.death_penalty_type) {
  6853. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6854. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6855. }
  6856. if (job_penalty) {
  6857. if (battle_config.pk_mode && src && src->type==BL_PC)
  6858. job_penalty *= 2;
  6859. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6860. }
  6861. }
  6862. else
  6863. job_penalty = 0;
  6864. if (base_penalty || job_penalty)
  6865. pc_lostexp(sd, base_penalty, job_penalty);
  6866. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6867. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6868. if(zeny_penalty)
  6869. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6870. }
  6871. }
  6872. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6873. int j;
  6874. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6875. int id = map[sd->bl.m].drop_list[j].drop_id;
  6876. int type = map[sd->bl.m].drop_list[j].drop_type;
  6877. int per = map[sd->bl.m].drop_list[j].drop_per;
  6878. if(id == 0)
  6879. continue;
  6880. if(id == -1){
  6881. int eq_num=0,eq_n[MAX_INVENTORY];
  6882. memset(eq_n,0,sizeof(eq_n));
  6883. for(i=0;i<MAX_INVENTORY;i++) {
  6884. if( (type == 1 && !sd->status.inventory[i].equip)
  6885. || (type == 2 && sd->status.inventory[i].equip)
  6886. || type == 3)
  6887. {
  6888. int l;
  6889. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6890. if( l < MAX_INVENTORY )
  6891. eq_n[l] = i;
  6892. eq_num++;
  6893. }
  6894. }
  6895. if(eq_num > 0){
  6896. int n = eq_n[rnd()%eq_num];
  6897. if(rnd()%10000 < per) {
  6898. if(sd->status.inventory[n].equip)
  6899. pc_unequipitem(sd,n,3);
  6900. pc_dropitem(sd,n,1);
  6901. }
  6902. }
  6903. }
  6904. else if(id > 0) {
  6905. for(i=0;i<MAX_INVENTORY;i++){
  6906. if(sd->status.inventory[i].nameid == id
  6907. && rnd()%10000 < per
  6908. && ((type == 1 && !sd->status.inventory[i].equip)
  6909. || (type == 2 && sd->status.inventory[i].equip)
  6910. || type == 3) ){
  6911. if(sd->status.inventory[i].equip)
  6912. pc_unequipitem(sd,i,3);
  6913. pc_dropitem(sd,i,1);
  6914. break;
  6915. }
  6916. }
  6917. }
  6918. }
  6919. }
  6920. // pvp
  6921. // disable certain pvp functions on pk_mode [Valaris]
  6922. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6923. sd->pvp_point -= 5;
  6924. sd->pvp_lost++;
  6925. if( src && src->type == BL_PC ) {
  6926. struct map_session_data *ssd = (struct map_session_data *)src;
  6927. ssd->pvp_point++;
  6928. ssd->pvp_won++;
  6929. }
  6930. if( sd->pvp_point < 0 ) {
  6931. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6932. return 1|8;
  6933. }
  6934. }
  6935. //GvG
  6936. if( map_flag_gvg(sd->bl.m) ) {
  6937. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6938. return 1|8;
  6939. }
  6940. else if( sd->bg_id ) {
  6941. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6942. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6943. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6944. return 1|8;
  6945. }
  6946. }
  6947. //Reset "can log out" tick.
  6948. if( battle_config.prevent_logout )
  6949. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6950. return 1;
  6951. }
  6952. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6953. if(hp) clif_updatestatus(sd,SP_HP);
  6954. if(sp) clif_updatestatus(sd,SP_SP);
  6955. pc_setstand(sd, true);
  6956. if(battle_config.pc_invincible_time > 0)
  6957. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6958. if( sd->state.gmaster_flag ) {
  6959. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  6960. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  6961. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  6962. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  6963. }
  6964. }
  6965. // script
  6966. //
  6967. /*==========================================
  6968. * script reading pc status registry
  6969. *------------------------------------------*/
  6970. int pc_readparam(struct map_session_data* sd,int type)
  6971. {
  6972. int val = 0;
  6973. nullpo_ret(sd);
  6974. switch(type) {
  6975. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6976. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6977. case SP_ZENY: val = sd->status.zeny; break;
  6978. case SP_BASELEVEL: val = sd->status.base_level; break;
  6979. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6980. case SP_CLASS: val = sd->status.class_; break;
  6981. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6982. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6983. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6984. case SP_SEX: val = sd->status.sex; break;
  6985. case SP_WEIGHT: val = sd->weight; break;
  6986. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6987. case SP_BASEEXP: val = sd->status.base_exp; break;
  6988. case SP_JOBEXP: val = sd->status.job_exp; break;
  6989. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6990. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6991. case SP_HP: val = sd->battle_status.hp; break;
  6992. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6993. case SP_SP: val = sd->battle_status.sp; break;
  6994. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6995. case SP_STR: val = sd->status.str; break;
  6996. case SP_AGI: val = sd->status.agi; break;
  6997. case SP_VIT: val = sd->status.vit; break;
  6998. case SP_INT: val = sd->status.int_; break;
  6999. case SP_DEX: val = sd->status.dex; break;
  7000. case SP_LUK: val = sd->status.luk; break;
  7001. case SP_KARMA: val = sd->status.karma; break;
  7002. case SP_MANNER: val = sd->status.manner; break;
  7003. case SP_FAME: val = sd->status.fame; break;
  7004. case SP_KILLERRID: val = sd->killerrid; break;
  7005. case SP_KILLEDRID: val = sd->killedrid; break;
  7006. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7007. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7008. case SP_CHARRENAME: val = sd->status.rename; break;
  7009. case SP_CHARFONT: val = sd->status.font; break;
  7010. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7011. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7012. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7013. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7014. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7015. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7016. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7017. case SP_DEF1: val = sd->battle_status.def; break;
  7018. case SP_DEF2: val = sd->battle_status.def2; break;
  7019. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7020. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7021. case SP_HIT: val = sd->battle_status.hit; break;
  7022. case SP_FLEE1: val = sd->battle_status.flee; break;
  7023. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7024. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7025. case SP_MAXHPRATE: val = sd->hprate; break;
  7026. case SP_MAXSPRATE: val = sd->sprate; break;
  7027. case SP_SPRATE: val = sd->dsprate; break;
  7028. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7029. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7030. case SP_ASPD_RATE:
  7031. #ifndef RENEWAL_ASPD
  7032. val = sd->battle_status.aspd_rate;
  7033. #else
  7034. val = sd->battle_status.aspd_rate2;
  7035. #endif
  7036. break;
  7037. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7038. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7039. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7040. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7041. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7042. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7043. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7044. case SP_MATK_RATE: val = sd->matk_rate; break;
  7045. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7046. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7047. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7048. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7049. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7050. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7051. case SP_HIT_RATE: val = sd->hit_rate; break;
  7052. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7053. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7054. case SP_DEF_RATE: val = sd->def_rate; break;
  7055. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7056. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7057. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7058. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7059. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7060. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7061. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7062. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7063. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7064. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7065. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7066. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7067. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7068. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7069. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7070. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7071. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7072. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7073. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7074. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7075. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7076. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7077. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7078. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7079. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7080. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7081. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7082. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7083. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7084. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7085. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7086. case SP_DELAYRATE: val = sd->delayrate; break;
  7087. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7088. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7089. case SP_UNSTRIPABLE:
  7090. case SP_UNSTRIPABLE_ARMOR:
  7091. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7092. break;
  7093. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7094. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7095. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7096. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7097. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7098. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7099. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7100. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7101. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7102. case SP_EMATK: val = sd->bonus.ematk; break;
  7103. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7104. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7105. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7106. case SP_CASTRATE:
  7107. case SP_VARCASTRATE:
  7108. #ifdef RENEWAL_CAST
  7109. val = sd->bonus.varcastrate; break;
  7110. #else
  7111. val = sd->castrate; break;
  7112. #endif
  7113. default:
  7114. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7115. return -1;
  7116. }
  7117. return val;
  7118. }
  7119. /*==========================================
  7120. * script set pc status registry
  7121. *------------------------------------------*/
  7122. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7123. {
  7124. nullpo_retr(false,sd);
  7125. switch(type){
  7126. case SP_BASELEVEL:
  7127. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7128. val = pc_maxbaselv(sd);
  7129. if ((unsigned int)val > sd->status.base_level) {
  7130. int i = 0;
  7131. int stat=0;
  7132. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7133. stat += pc_gets_status_point(sd->status.base_level + i);
  7134. sd->status.status_point += stat;
  7135. }
  7136. sd->status.base_level = (unsigned int)val;
  7137. sd->status.base_exp = 0;
  7138. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7139. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7140. clif_updatestatus(sd, SP_STATUSPOINT);
  7141. clif_updatestatus(sd, SP_BASEEXP);
  7142. status_calc_pc(sd, SCO_FORCE);
  7143. if(sd->status.party_id)
  7144. party_send_levelup(sd);
  7145. break;
  7146. case SP_JOBLEVEL:
  7147. if ((unsigned int)val >= sd->status.job_level) {
  7148. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7149. sd->status.skill_point += val - sd->status.job_level;
  7150. clif_updatestatus(sd, SP_SKILLPOINT);
  7151. }
  7152. sd->status.job_level = (unsigned int)val;
  7153. sd->status.job_exp = 0;
  7154. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7155. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7156. clif_updatestatus(sd, SP_JOBEXP);
  7157. status_calc_pc(sd, SCO_FORCE);
  7158. break;
  7159. case SP_SKILLPOINT:
  7160. sd->status.skill_point = val;
  7161. break;
  7162. case SP_STATUSPOINT:
  7163. sd->status.status_point = val;
  7164. break;
  7165. case SP_ZENY:
  7166. if( val < 0 )
  7167. return false;// can't set negative zeny
  7168. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7169. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7170. break;
  7171. case SP_BASEEXP:
  7172. {
  7173. val = cap_value(val, 0, INT_MAX);
  7174. if ((unsigned int)val < sd->status.base_exp) // Lost
  7175. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7176. else // Gained
  7177. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7178. }
  7179. return true;
  7180. case SP_JOBEXP:
  7181. {
  7182. val = cap_value(val, 0, INT_MAX);
  7183. if ((unsigned int)val < sd->status.job_exp) // Lost
  7184. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7185. else // Gained
  7186. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7187. }
  7188. return true;
  7189. case SP_SEX:
  7190. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7191. break;
  7192. case SP_WEIGHT:
  7193. sd->weight = val;
  7194. break;
  7195. case SP_MAXWEIGHT:
  7196. sd->max_weight = val;
  7197. break;
  7198. case SP_HP:
  7199. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7200. break;
  7201. case SP_MAXHP:
  7202. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7203. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7204. {
  7205. sd->battle_status.hp = sd->battle_status.max_hp;
  7206. clif_updatestatus(sd, SP_HP);
  7207. }
  7208. break;
  7209. case SP_SP:
  7210. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7211. break;
  7212. case SP_MAXSP:
  7213. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7214. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7215. {
  7216. sd->battle_status.sp = sd->battle_status.max_sp;
  7217. clif_updatestatus(sd, SP_SP);
  7218. }
  7219. break;
  7220. case SP_STR:
  7221. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7222. break;
  7223. case SP_AGI:
  7224. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7225. break;
  7226. case SP_VIT:
  7227. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7228. break;
  7229. case SP_INT:
  7230. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7231. break;
  7232. case SP_DEX:
  7233. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7234. break;
  7235. case SP_LUK:
  7236. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7237. break;
  7238. case SP_KARMA:
  7239. sd->status.karma = val;
  7240. break;
  7241. case SP_MANNER:
  7242. sd->status.manner = val;
  7243. if( val < 0 )
  7244. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7245. else {
  7246. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7247. clif_manner_message(sd, 5);
  7248. }
  7249. return true; // status_change_start/status_change_end already sends packets warning the client
  7250. case SP_FAME:
  7251. sd->status.fame = val;
  7252. break;
  7253. case SP_KILLERRID:
  7254. sd->killerrid = val;
  7255. return true;
  7256. case SP_KILLEDRID:
  7257. sd->killedrid = val;
  7258. return true;
  7259. case SP_CHARMOVE:
  7260. sd->status.character_moves = val;
  7261. return true;
  7262. case SP_CHARRENAME:
  7263. sd->status.rename = val;
  7264. return true;
  7265. case SP_CHARFONT:
  7266. sd->status.font = val;
  7267. clif_font(sd);
  7268. return true;
  7269. case SP_BANK_VAULT:
  7270. if (val < 0)
  7271. return false;
  7272. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7273. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7274. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7275. return true;
  7276. case SP_ROULETTE_BRONZE:
  7277. sd->roulette_point.bronze = val;
  7278. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7279. return true;
  7280. case SP_ROULETTE_SILVER:
  7281. sd->roulette_point.silver = val;
  7282. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7283. return true;
  7284. case SP_ROULETTE_GOLD:
  7285. sd->roulette_point.gold = val;
  7286. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7287. return true;
  7288. default:
  7289. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7290. return false;
  7291. }
  7292. clif_updatestatus(sd,type);
  7293. return true;
  7294. }
  7295. /*==========================================
  7296. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7297. *------------------------------------------*/
  7298. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7299. {
  7300. if (type) {
  7301. if (hp)
  7302. clif_heal(sd->fd,SP_HP,hp);
  7303. if (sp)
  7304. clif_heal(sd->fd,SP_SP,sp);
  7305. } else {
  7306. if(hp)
  7307. clif_updatestatus(sd,SP_HP);
  7308. if(sp)
  7309. clif_updatestatus(sd,SP_SP);
  7310. }
  7311. return;
  7312. }
  7313. /*==========================================
  7314. * HP/SP Recovery
  7315. * Heal player hp and/or sp linearly.
  7316. * Calculate bonus by status.
  7317. *------------------------------------------*/
  7318. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  7319. {
  7320. int bonus, tmp, penalty = 0;
  7321. if(hp) {
  7322. int i;
  7323. bonus = 100 + (sd->battle_status.vit<<1)
  7324. + pc_checkskill(sd,SM_RECOVERY)*10
  7325. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7326. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7327. if (potion_flag > 1)
  7328. bonus += bonus*(potion_flag-1)*50/100;
  7329. //All item bonuses.
  7330. bonus += sd->bonus.itemhealrate2;
  7331. //Item Group bonuses
  7332. bonus += bonus*pc_get_itemgroup_bonus(sd, itemid)/100;
  7333. //Individual item bonuses.
  7334. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  7335. {
  7336. if (sd->itemhealrate[i].nameid == itemid) {
  7337. bonus += bonus*sd->itemhealrate[i].rate/100;
  7338. break;
  7339. }
  7340. }
  7341. tmp = hp * bonus / 100; // overflow check
  7342. if(bonus != 100 && tmp > hp)
  7343. hp = tmp;
  7344. // Recovery Potion
  7345. if( sd->sc.data[SC_INCHEALRATE] )
  7346. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  7347. // 2014 Halloween Event : Pumpkin Bonus
  7348. if(sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7349. hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100.);
  7350. }
  7351. if(sp) {
  7352. bonus = 100 + (sd->battle_status.int_<<1)
  7353. + pc_checkskill(sd,MG_SRECOVERY)*10
  7354. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7355. if (potion_flag > 1)
  7356. bonus += bonus*(potion_flag-1)*50/100;
  7357. tmp = sp * bonus / 100;
  7358. if(bonus != 100 && tmp > sp)
  7359. sp = tmp;
  7360. }
  7361. if (sd->sc.count) {
  7362. // Critical Wound and Death Hurt stack
  7363. if (sd->sc.data[SC_CRITICALWOUND])
  7364. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7365. if (sd->sc.data[SC_DEATHHURT])
  7366. penalty += 20;
  7367. if (sd->sc.data[SC_NORECOVER_STATE])
  7368. penalty = 100;
  7369. if (penalty > 0) {
  7370. hp -= hp * penalty / 100;
  7371. sp -= sp * penalty / 100;
  7372. }
  7373. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7374. hp += hp / 2; // 1.5 times
  7375. sp -= sp / 2;
  7376. }
  7377. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7378. hp += hp / 10;
  7379. sp += sp / 10;
  7380. }
  7381. #ifdef RENEWAL
  7382. if (sd->sc.data[SC_EXTREMITYFIST2])
  7383. sp = 0;
  7384. #endif
  7385. if (sd->sc.data[SC_BITESCAR])
  7386. hp = 0;
  7387. }
  7388. return status_heal(&sd->bl, hp, sp, 1);
  7389. }
  7390. /*==========================================
  7391. * HP/SP Recovery
  7392. * Heal player hp nad/or sp by rate
  7393. *------------------------------------------*/
  7394. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7395. {
  7396. nullpo_ret(sd);
  7397. if (hp > 100) hp = 100;
  7398. else if (hp <-100) hp = -100;
  7399. if (sp > 100) sp = 100;
  7400. else if (sp <-100) sp = -100;
  7401. if(hp >= 0 && sp >= 0) //Heal
  7402. return status_percent_heal(&sd->bl, hp, sp);
  7403. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7404. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7405. //Crossed signs
  7406. if(hp) {
  7407. if(hp > 0)
  7408. status_percent_heal(&sd->bl, hp, 0);
  7409. else
  7410. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7411. }
  7412. if(sp) {
  7413. if(sp > 0)
  7414. status_percent_heal(&sd->bl, 0, sp);
  7415. else
  7416. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7417. }
  7418. return 0;
  7419. }
  7420. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7421. {
  7422. struct mob_data *md;
  7423. int flag;
  7424. md = (struct mob_data *)bl;
  7425. nullpo_ret(md);
  7426. flag = va_arg(ap, int);
  7427. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7428. status_kill(&md->bl);
  7429. return 1;
  7430. }
  7431. /**
  7432. * Called when player changes job
  7433. * Rewrote to make it tidider [Celest]
  7434. * @param sd
  7435. * @param job JOB ID. See enum e_job
  7436. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7437. * @return True if success, false if failed
  7438. **/
  7439. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7440. {
  7441. int i, fame_flag = 0;
  7442. int b_class;
  7443. nullpo_retr(false,sd);
  7444. if (job < 0)
  7445. return false;
  7446. //Normalize job.
  7447. b_class = pc_jobid2mapid(job);
  7448. if (b_class == -1)
  7449. return false;
  7450. switch (upper) {
  7451. case 1:
  7452. b_class|= JOBL_UPPER;
  7453. break;
  7454. case 2:
  7455. b_class|= JOBL_BABY;
  7456. break;
  7457. }
  7458. //This will automatically adjust bard/dancer classes to the correct gender
  7459. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7460. job = pc_mapid2jobid(b_class, sd->status.sex);
  7461. if (job == -1)
  7462. return false;
  7463. if ((unsigned short)b_class == sd->class_)
  7464. return false; //Nothing to change.
  7465. // changing from 1st to 2nd job
  7466. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7467. sd->change_level_2nd = sd->status.job_level;
  7468. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  7469. }
  7470. // changing from 2nd to 3rd job
  7471. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7472. sd->change_level_3rd = sd->status.job_level;
  7473. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  7474. }
  7475. if(sd->cloneskill_idx > 0) {
  7476. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7477. sd->status.skill[sd->cloneskill_idx].id = 0;
  7478. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7479. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7480. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7481. }
  7482. sd->cloneskill_idx = 0;
  7483. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7484. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7485. }
  7486. if(sd->reproduceskill_idx > 0) {
  7487. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7488. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7489. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7490. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7491. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7492. }
  7493. sd->reproduceskill_idx = 0;
  7494. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7495. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7496. }
  7497. // Give or reduce transcendent status points
  7498. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7499. sd->status.status_point += battle_config.transcendent_status_points;
  7500. clif_updatestatus(sd,SP_STATUSPOINT);
  7501. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7502. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7503. // The player already used his bonus points, so we have to reset his status points
  7504. pc_resetstate(sd);
  7505. }
  7506. sd->status.status_point -= battle_config.transcendent_status_points;
  7507. clif_updatestatus(sd,SP_STATUSPOINT);
  7508. }
  7509. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7510. const int class_ = pc_class2idx(sd->status.class_);
  7511. uint16 skill_id;
  7512. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7513. //Remove status specific to your current tree skills.
  7514. enum sc_type sc = status_skill2sc(skill_id);
  7515. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7516. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7517. }
  7518. }
  7519. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7520. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7521. pc_resetfeel(sd);
  7522. }
  7523. // Reset body style to 0 before changing job to avoid
  7524. // errors since not every job has a alternate outfit.
  7525. sd->status.body = 0;
  7526. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7527. sd->status.class_ = job;
  7528. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7529. sd->class_ = (unsigned short)b_class;
  7530. sd->status.job_level=1;
  7531. sd->status.job_exp=0;
  7532. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7533. sd->status.base_level = pc_maxbaselv(sd);
  7534. sd->status.base_exp=0;
  7535. pc_resetstate(sd);
  7536. clif_updatestatus(sd,SP_STATUSPOINT);
  7537. clif_updatestatus(sd,SP_BASELEVEL);
  7538. clif_updatestatus(sd,SP_BASEEXP);
  7539. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7540. }
  7541. clif_updatestatus(sd,SP_JOBLEVEL);
  7542. clif_updatestatus(sd,SP_JOBEXP);
  7543. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7544. for(i=0;i<EQI_MAX;i++) {
  7545. if(sd->equip_index[i] >= 0)
  7546. if(pc_isequip(sd,sd->equip_index[i]))
  7547. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7548. }
  7549. //Change look, if disguised, you need to undisguise
  7550. //to correctly calculate new job sprite without
  7551. if (sd->disguise)
  7552. pc_disguise(sd, 0);
  7553. status_set_viewdata(&sd->bl, job);
  7554. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7555. #if PACKETVER >= 20151001
  7556. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7557. #endif
  7558. if(sd->vd.cloth_color)
  7559. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7560. /*
  7561. if(sd->vd.body_style)
  7562. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7563. */
  7564. //Update skill tree.
  7565. pc_calc_skilltree(sd);
  7566. clif_skillinfoblock(sd);
  7567. if (sd->ed)
  7568. elemental_delete(sd->ed);
  7569. if (sd->state.vending)
  7570. vending_closevending(sd);
  7571. if (sd->state.buyingstore)
  7572. buyingstore_close(sd);
  7573. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7574. //Remove peco/cart/falcon
  7575. i = sd->sc.option;
  7576. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7577. i&=~OPTION_RIDING;
  7578. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7579. i&=~OPTION_FALCON;
  7580. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7581. i&=~OPTION_DRAGON;
  7582. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7583. i&=~OPTION_WUGRIDER;
  7584. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7585. i&=~OPTION_WUG;
  7586. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7587. i&=~OPTION_MADOGEAR;
  7588. #ifndef NEW_CARTS
  7589. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7590. i&=~OPTION_CART;
  7591. #else
  7592. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7593. pc_setcart(sd, 0);
  7594. #endif
  7595. if(i != sd->sc.option)
  7596. pc_setoption(sd, i);
  7597. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7598. hom_vaporize(sd, HOM_ST_ACTIVE);
  7599. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7600. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7601. if(sd->status.manner < 0)
  7602. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7603. status_calc_pc(sd,SCO_FORCE);
  7604. pc_checkallowskill(sd);
  7605. pc_equiplookall(sd);
  7606. pc_show_questinfo(sd);
  7607. //if you were previously famous, not anymore.
  7608. if (fame_flag) {
  7609. chrif_save(sd,0);
  7610. chrif_buildfamelist();
  7611. } else if (sd->status.fame > 0) {
  7612. //It may be that now they are famous?
  7613. switch (sd->class_&MAPID_UPPERMASK) {
  7614. case MAPID_BLACKSMITH:
  7615. case MAPID_ALCHEMIST:
  7616. case MAPID_TAEKWON:
  7617. chrif_save(sd,0);
  7618. chrif_buildfamelist();
  7619. break;
  7620. }
  7621. }
  7622. return true;
  7623. }
  7624. /*==========================================
  7625. * Tell client player sd has change equipement
  7626. *------------------------------------------*/
  7627. void pc_equiplookall(struct map_session_data *sd)
  7628. {
  7629. nullpo_retv(sd);
  7630. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7631. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7632. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7633. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7634. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7635. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7636. }
  7637. /*==========================================
  7638. * Tell client player sd has change look (hair,equip...)
  7639. *------------------------------------------*/
  7640. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7641. nullpo_retv(sd);
  7642. switch(type) {
  7643. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7644. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7645. if (sd->status.hair != val) {
  7646. sd->status.hair = val;
  7647. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7648. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7649. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7650. }
  7651. break;
  7652. case LOOK_WEAPON:
  7653. sd->status.weapon = val;
  7654. break;
  7655. case LOOK_HEAD_BOTTOM:
  7656. sd->status.head_bottom = val;
  7657. sd->setlook_head_bottom = val;
  7658. break;
  7659. case LOOK_HEAD_TOP:
  7660. sd->status.head_top = val;
  7661. sd->setlook_head_top = val;
  7662. break;
  7663. case LOOK_HEAD_MID:
  7664. sd->status.head_mid = val;
  7665. sd->setlook_head_mid = val;
  7666. break;
  7667. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7668. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7669. if (sd->status.hair_color != val) {
  7670. sd->status.hair_color = val;
  7671. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7672. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7673. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7674. }
  7675. break;
  7676. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7677. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7678. sd->status.clothes_color = val;
  7679. break;
  7680. case LOOK_SHIELD:
  7681. sd->status.shield = val;
  7682. break;
  7683. case LOOK_SHOES:
  7684. break;
  7685. case LOOK_ROBE:
  7686. sd->status.robe = val;
  7687. sd->setlook_robe = val;
  7688. break;
  7689. case LOOK_BODY2:
  7690. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7691. sd->status.body = val;
  7692. break;
  7693. }
  7694. clif_changelook(&sd->bl, type, val);
  7695. }
  7696. /*==========================================
  7697. * Give an option (type) to player (sd) and display it to client
  7698. *------------------------------------------*/
  7699. void pc_setoption(struct map_session_data *sd,int type)
  7700. {
  7701. int p_type, new_look=0;
  7702. nullpo_retv(sd);
  7703. p_type = sd->sc.option;
  7704. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7705. sd->sc.option=type;
  7706. clif_changeoption(&sd->bl);
  7707. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7708. { // Mounting
  7709. clif_status_load(&sd->bl,SI_RIDING,1);
  7710. status_calc_pc(sd,SCO_NONE);
  7711. }
  7712. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7713. { // Dismount
  7714. clif_status_load(&sd->bl,SI_RIDING,0);
  7715. status_calc_pc(sd,SCO_NONE);
  7716. }
  7717. #ifndef NEW_CARTS
  7718. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7719. clif_cartlist(sd);
  7720. clif_updatestatus(sd, SP_CARTINFO);
  7721. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7722. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7723. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7724. clif_clearcart(sd->fd);
  7725. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7726. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7727. }
  7728. #endif
  7729. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7730. clif_status_load(&sd->bl,SI_FALCON,1);
  7731. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7732. clif_status_load(&sd->bl,SI_FALCON,0);
  7733. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7734. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7735. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7736. status_calc_pc(sd,SCO_NONE);
  7737. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7738. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7739. status_calc_pc(sd,SCO_NONE);
  7740. }
  7741. }
  7742. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7743. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7744. status_calc_pc(sd,SCO_NONE);
  7745. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7746. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7747. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7748. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7749. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7750. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7751. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7752. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7753. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7754. status_calc_pc(sd,SCO_NONE);
  7755. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7756. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7757. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7758. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7759. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7760. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7761. }
  7762. }
  7763. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7764. new_look = JOB_STAR_GLADIATOR2;
  7765. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7766. new_look = -1;
  7767. if (sd->disguise || !new_look)
  7768. return; //Disguises break sprite changes
  7769. if (new_look < 0) { //Restore normal look.
  7770. status_set_viewdata(&sd->bl, sd->status.class_);
  7771. new_look = sd->vd.class_;
  7772. }
  7773. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7774. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7775. if (sd->vd.cloth_color)
  7776. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7777. if( sd->vd.body_style )
  7778. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7779. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7780. }
  7781. /**
  7782. * Give player a cart
  7783. * @param sd Player
  7784. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  7785. **/
  7786. bool pc_setcart(struct map_session_data *sd,int type) {
  7787. #ifndef NEW_CARTS
  7788. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7789. int option;
  7790. #endif
  7791. nullpo_retr(false,sd);
  7792. if( type < 0 || type > MAX_CARTS )
  7793. return false;// Never trust the values sent by the client! [Skotlex]
  7794. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7795. return false;// Push cart is required
  7796. #ifdef NEW_CARTS
  7797. switch( type ) {
  7798. case 0:
  7799. if( !sd->sc.data[SC_PUSH_CART] )
  7800. return 0;
  7801. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7802. clif_clearcart(sd->fd);
  7803. break;
  7804. default:/* everything else is an allowed ID so we can move on */
  7805. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7806. clif_cartlist(sd);
  7807. clif_updatestatus(sd, SP_CARTINFO);
  7808. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  7809. break;
  7810. }
  7811. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7812. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7813. #else
  7814. // Update option
  7815. option = sd->sc.option;
  7816. option &= ~OPTION_CART;// clear cart bits
  7817. option |= cart[type]; // set cart
  7818. pc_setoption(sd, option);
  7819. #endif
  7820. return true;
  7821. }
  7822. /*==========================================
  7823. * Give player a falcon
  7824. *------------------------------------------*/
  7825. void pc_setfalcon(TBL_PC* sd, int flag)
  7826. {
  7827. if( flag ){
  7828. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7829. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7830. } else if( pc_isfalcon(sd) ){
  7831. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7832. }
  7833. }
  7834. /*==========================================
  7835. * Set player riding
  7836. *------------------------------------------*/
  7837. void pc_setriding(TBL_PC* sd, int flag)
  7838. {
  7839. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7840. return;
  7841. if( flag ){
  7842. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7843. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7844. } else if( pc_isriding(sd) ){
  7845. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7846. }
  7847. }
  7848. /*==========================================
  7849. * Give player a mado
  7850. *------------------------------------------*/
  7851. void pc_setmadogear(TBL_PC* sd, int flag)
  7852. {
  7853. if( flag ){
  7854. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7855. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7856. } else if( pc_ismadogear(sd) ){
  7857. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7858. }
  7859. }
  7860. /*==========================================
  7861. * Check if player can drop an item
  7862. *------------------------------------------*/
  7863. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7864. {
  7865. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7866. return false;
  7867. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7868. return false;
  7869. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7870. }
  7871. /**
  7872. * Determines whether a player can attack based on status changes
  7873. * Why not use status_check_skilluse?
  7874. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7875. * Even ground-based attacks should be blocked by these statuses
  7876. * Called from unit_attack and unit_attack_timer_sub
  7877. * @retval true Can attack
  7878. **/
  7879. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7880. nullpo_retr(false, sd);
  7881. if (!&sd->sc)
  7882. return true;
  7883. if( sd->sc.data[SC_BASILICA] ||
  7884. sd->sc.data[SC__SHADOWFORM] ||
  7885. sd->sc.data[SC__MANHOLE] ||
  7886. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7887. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7888. sd->sc.data[SC_CRYSTALIZE] ||
  7889. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7890. sd->sc.data[SC_TRICKDEAD] ||
  7891. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7892. sd->sc.data[SC_BLADESTOP] ||
  7893. sd->sc.data[SC_DEEPSLEEP] ||
  7894. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  7895. sd->sc.data[SC_KINGS_GRACE] )
  7896. return false;
  7897. return true;
  7898. }
  7899. /*==========================================
  7900. * Read '@type' variables (temporary numeric char reg)
  7901. *------------------------------------------*/
  7902. int pc_readreg(struct map_session_data* sd, int64 reg)
  7903. {
  7904. return i64db_iget(sd->regs.vars, reg);
  7905. }
  7906. /*==========================================
  7907. * Set '@type' variables (temporary numeric char reg)
  7908. *------------------------------------------*/
  7909. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  7910. {
  7911. unsigned int index = script_getvaridx(reg);
  7912. nullpo_retr(false, sd);
  7913. if( val ) {
  7914. i64db_iput(sd->regs.vars, reg, val);
  7915. if( index )
  7916. script_array_update(&sd->regs, reg, false);
  7917. } else {
  7918. i64db_remove(sd->regs.vars, reg);
  7919. if( index )
  7920. script_array_update(&sd->regs, reg, true);
  7921. }
  7922. return true;
  7923. }
  7924. /*==========================================
  7925. * Read '@type$' variables (temporary string char reg)
  7926. *------------------------------------------*/
  7927. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  7928. {
  7929. struct script_reg_str *p = NULL;
  7930. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7931. return p ? p->value : NULL;
  7932. }
  7933. /*==========================================
  7934. * Set '@type$' variables (temporary string char reg)
  7935. *------------------------------------------*/
  7936. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  7937. {
  7938. struct script_reg_str *p = NULL;
  7939. unsigned int index = script_getvaridx(reg);
  7940. DBData prev;
  7941. nullpo_retr(false, sd);
  7942. if( str[0] ) {
  7943. p = ers_alloc(str_reg_ers, struct script_reg_str);
  7944. p->value = aStrdup(str);
  7945. p->flag.type = 1;
  7946. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7947. p = (struct script_reg_str *)db_data2ptr(&prev);
  7948. if( p->value )
  7949. aFree(p->value);
  7950. ers_free(str_reg_ers, p);
  7951. } else {
  7952. if( index )
  7953. script_array_update(&sd->regs, reg, false);
  7954. }
  7955. } else {
  7956. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  7957. p = (struct script_reg_str *)db_data2ptr(&prev);
  7958. if( p->value )
  7959. aFree(p->value);
  7960. ers_free(str_reg_ers, p);
  7961. if( index )
  7962. script_array_update(&sd->regs, reg, true);
  7963. }
  7964. }
  7965. return true;
  7966. }
  7967. /**
  7968. * Serves the following variable types:
  7969. * - 'type' (permanent numeric char reg)
  7970. * - '#type' (permanent numeric account reg)
  7971. * - '##type' (permanent numeric account reg2)
  7972. **/
  7973. int pc_readregistry(struct map_session_data *sd, int64 reg)
  7974. {
  7975. struct script_reg_num *p = NULL;
  7976. if (!sd->vars_ok) {
  7977. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7978. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7979. //intif->request_registry(sd,type==3?4:type);
  7980. set_eof(sd->fd);
  7981. return 0;
  7982. }
  7983. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  7984. return p ? p->value : 0;
  7985. }
  7986. /**
  7987. * Serves the following variable types:
  7988. * - 'type$' (permanent str char reg)
  7989. * - '#type$' (permanent str account reg)
  7990. * - '##type$' (permanent str account reg2)
  7991. **/
  7992. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  7993. {
  7994. struct script_reg_str *p = NULL;
  7995. if (!sd->vars_ok) {
  7996. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7997. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7998. //intif->request_registry(sd,type==3?4:type);
  7999. set_eof(sd->fd);
  8000. return NULL;
  8001. }
  8002. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8003. return p ? p->value : NULL;
  8004. }
  8005. /**
  8006. * Serves the following variable types:
  8007. * - 'type' (permanent numeric char reg)
  8008. * - '#type' (permanent numeric account reg)
  8009. * - '##type' (permanent numeric account reg2)
  8010. **/
  8011. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8012. {
  8013. struct script_reg_num *p = NULL;
  8014. const char *regname = get_str(script_getvarid(reg));
  8015. unsigned int index = script_getvaridx(reg);
  8016. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  8017. switch( regname[0] ) {
  8018. default: //Char reg
  8019. if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  8020. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  8021. sd->die_counter = val;
  8022. if( i )
  8023. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  8024. } else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
  8025. val = cap_value(val, 0, 1999);
  8026. sd->cook_mastery = val;
  8027. }
  8028. break;
  8029. case '#':
  8030. if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
  8031. val = cap_value(val, 0, MAX_ZENY);
  8032. sd->cashPoints = val;
  8033. } else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  8034. val = cap_value(val, 0, MAX_ZENY);
  8035. sd->kafraPoints = val;
  8036. }
  8037. break;
  8038. }
  8039. if ( !reg_load && !sd->vars_ok ) {
  8040. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8041. return 0;
  8042. }
  8043. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8044. if( val ) {
  8045. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8046. script_array_update(&sd->regs, reg, false);
  8047. p->value = val;
  8048. } else {
  8049. p->value = 0;
  8050. if( index )
  8051. script_array_update(&sd->regs, reg, true);
  8052. }
  8053. if (!reg_load)
  8054. p->flag.update = 1;/* either way, it will require either delete or replace */
  8055. } else if( val ) {
  8056. DBData prev;
  8057. if( index )
  8058. script_array_update(&sd->regs, reg, false);
  8059. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8060. p->value = val;
  8061. if (!reg_load)
  8062. p->flag.update = 1;
  8063. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8064. p = (struct script_reg_num *)db_data2ptr(&prev);
  8065. ers_free(num_reg_ers, p);
  8066. }
  8067. }
  8068. if (!reg_load && p)
  8069. sd->vars_dirty = true;
  8070. return 1;
  8071. }
  8072. /**
  8073. * Serves the following variable types:
  8074. * - 'type$' (permanent str char reg)
  8075. * - '#type$' (permanent str account reg)
  8076. * - '##type$' (permanent str account reg2)
  8077. **/
  8078. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8079. {
  8080. struct script_reg_str *p = NULL;
  8081. const char *regname = get_str(script_getvarid(reg));
  8082. unsigned int index = script_getvaridx(reg);
  8083. if (!reg_load && !sd->vars_ok) {
  8084. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8085. return 0;
  8086. }
  8087. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8088. if( val[0] ) {
  8089. if( p->value )
  8090. aFree(p->value);
  8091. else if ( index ) // an entry that was deleted, so we reset
  8092. script_array_update(&sd->regs, reg, false);
  8093. p->value = aStrdup(val);
  8094. } else {
  8095. if (p->value)
  8096. aFree(p->value);
  8097. p->value = NULL;
  8098. if( index )
  8099. script_array_update(&sd->regs, reg, true);
  8100. }
  8101. if( !reg_load )
  8102. p->flag.update = 1; // either way, it will require either delete or replace
  8103. } else if( val[0] ) {
  8104. DBData prev;
  8105. if( index )
  8106. script_array_update(&sd->regs, reg, false);
  8107. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8108. p->value = aStrdup(val);
  8109. if( !reg_load )
  8110. p->flag.update = 1;
  8111. p->flag.type = 1;
  8112. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8113. p = (struct script_reg_str *)db_data2ptr(&prev);
  8114. if( p->value )
  8115. aFree(p->value);
  8116. ers_free(str_reg_ers, p);
  8117. }
  8118. }
  8119. if( !reg_load && p )
  8120. sd->vars_dirty = true;
  8121. return 1;
  8122. }
  8123. /**
  8124. * Set value of player variable
  8125. * @param sd Player
  8126. * @param reg Variable name
  8127. * @param value
  8128. * @return True if success, false if failed.
  8129. **/
  8130. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8131. char prefix = reg[0];
  8132. nullpo_retr(false, sd);
  8133. if (reg[strlen(reg)-1] == '$') {
  8134. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8135. return false;
  8136. }
  8137. val = cap_value(val, INT_MIN, INT_MAX);
  8138. switch (prefix) {
  8139. case '.':
  8140. case '\'':
  8141. case '$':
  8142. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8143. return false;
  8144. case '@':
  8145. return pc_setreg(sd, add_str(reg), val);
  8146. case '#':
  8147. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8148. default:
  8149. return pc_setglobalreg(sd, add_str(reg), val);
  8150. }
  8151. return false;
  8152. }
  8153. /**
  8154. * Get value of player variable
  8155. * @param sd Player
  8156. * @param reg Variable name
  8157. * @return Variable value or 0 if failed.
  8158. **/
  8159. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8160. char prefix = reg[0];
  8161. nullpo_ret(sd);
  8162. if (reg[strlen(reg)-1] == '$') {
  8163. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8164. return 0;
  8165. }
  8166. switch (prefix) {
  8167. case '.':
  8168. case '\'':
  8169. case '$':
  8170. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8171. return 0;
  8172. case '@':
  8173. return pc_readreg(sd, add_str(reg));
  8174. case '#':
  8175. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8176. default:
  8177. return pc_readglobalreg(sd, add_str(reg));
  8178. }
  8179. return 0;
  8180. }
  8181. /*==========================================
  8182. * Exec eventtimer for player sd (retrieved from map_session (id))
  8183. *------------------------------------------*/
  8184. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8185. {
  8186. struct map_session_data *sd=map_id2sd(id);
  8187. char *p = (char *)data;
  8188. int i;
  8189. if(sd==NULL)
  8190. return 0;
  8191. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8192. if( i < MAX_EVENTTIMER )
  8193. {
  8194. sd->eventtimer[i] = INVALID_TIMER;
  8195. sd->eventcount--;
  8196. npc_event(sd,p,0);
  8197. }
  8198. else
  8199. ShowError("pc_eventtimer: no such event timer\n");
  8200. if (p) aFree(p);
  8201. return 0;
  8202. }
  8203. /*==========================================
  8204. * Add eventtimer for player sd ?
  8205. *------------------------------------------*/
  8206. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8207. {
  8208. int i;
  8209. nullpo_retr(false,sd);
  8210. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8211. if( i == MAX_EVENTTIMER )
  8212. return false;
  8213. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8214. sd->eventcount++;
  8215. return true;
  8216. }
  8217. /*==========================================
  8218. * Del eventtimer for player sd ?
  8219. *------------------------------------------*/
  8220. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8221. {
  8222. char* p = NULL;
  8223. int i;
  8224. nullpo_retr(false,sd);
  8225. if (sd->eventcount == 0)
  8226. return false;
  8227. // find the named event timer
  8228. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8229. sd->eventtimer[i] != INVALID_TIMER &&
  8230. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8231. strcmp(p, name) == 0
  8232. );
  8233. if( i == MAX_EVENTTIMER )
  8234. return false; // not found
  8235. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8236. sd->eventtimer[i] = INVALID_TIMER;
  8237. if(sd->eventcount > 0)
  8238. sd->eventcount--;
  8239. aFree(p);
  8240. return true;
  8241. }
  8242. /*==========================================
  8243. * Update eventtimer count for player sd
  8244. *------------------------------------------*/
  8245. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8246. {
  8247. int i;
  8248. nullpo_retv(sd);
  8249. for(i=0;i<MAX_EVENTTIMER;i++)
  8250. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8251. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8252. addtick_timer(sd->eventtimer[i],tick);
  8253. break;
  8254. }
  8255. }
  8256. /*==========================================
  8257. * Remove all eventtimer for player sd
  8258. *------------------------------------------*/
  8259. void pc_cleareventtimer(struct map_session_data *sd)
  8260. {
  8261. int i;
  8262. nullpo_retv(sd);
  8263. if (sd->eventcount == 0)
  8264. return;
  8265. for(i=0;i<MAX_EVENTTIMER;i++){
  8266. if( sd->eventtimer[i] != INVALID_TIMER ){
  8267. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8268. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8269. sd->eventtimer[i] = INVALID_TIMER;
  8270. if(sd->eventcount > 0) //avoid looping to max val
  8271. sd->eventcount--;
  8272. if (p) aFree(p);
  8273. }
  8274. }
  8275. }
  8276. /**
  8277. * Called when an item with combo is worn
  8278. * @param *sd
  8279. * @param *data struct item_data
  8280. * @return success numbers of succeed combo
  8281. */
  8282. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8283. uint16 i;
  8284. int success = 0;
  8285. for( i = 0; i < data->combos_count; i++ ) {
  8286. struct itemchk {
  8287. int idx;
  8288. short card[MAX_SLOTS];
  8289. } *combo_idx;
  8290. int idx, j;
  8291. int nb_itemCombo;
  8292. unsigned int pos = 0;
  8293. /* ensure this isn't a duplicate combo */
  8294. if( sd->combos.bonus != NULL ) {
  8295. int x;
  8296. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8297. /* found a match, skip this combo */
  8298. if( x < sd->combos.count )
  8299. continue;
  8300. }
  8301. nb_itemCombo = data->combos[i]->count;
  8302. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8303. continue;
  8304. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8305. for(j=0; j < nb_itemCombo; j++){
  8306. combo_idx[j].idx=-1;
  8307. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8308. }
  8309. for( j = 0; j < nb_itemCombo; j++ ) {
  8310. uint16 id = data->combos[i]->nameid[j], k;
  8311. bool found = false;
  8312. for( k = 0; k < EQI_MAX; k++ ) {
  8313. short index = sd->equip_index[k];
  8314. if( index < 0 )
  8315. continue;
  8316. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8317. continue;
  8318. if (!sd->inventory_data[index] )
  8319. continue;
  8320. if ( itemdb_type(id) != IT_CARD ) {
  8321. if ( sd->inventory_data[index]->nameid != id )
  8322. continue;
  8323. if(j>0){ //check if this item not already used
  8324. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8325. uint8 z;
  8326. for (z = 0; z < nb_itemCombo-1; z++)
  8327. if(combo_idx[z].idx == index) //we already have that index recorded
  8328. do_continue=true;
  8329. if(do_continue)
  8330. continue;
  8331. }
  8332. combo_idx[j].idx = index;
  8333. pos |= sd->status.inventory[index].equip;
  8334. found = true;
  8335. break;
  8336. } else { //Cards
  8337. uint16 z;
  8338. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  8339. continue;
  8340. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  8341. bool do_continue=false;
  8342. if (sd->status.inventory[index].card[z] != id)
  8343. continue;
  8344. if(j>0){
  8345. int c1, c2;
  8346. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8347. if(combo_idx[c1].idx == index){
  8348. for (c2 = 0; c2 < sd->inventory_data[index]->slot; c2++){
  8349. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8350. do_continue = true;
  8351. break;
  8352. }
  8353. }
  8354. }
  8355. }
  8356. }
  8357. if(do_continue)
  8358. continue;
  8359. combo_idx[j].idx = index;
  8360. combo_idx[j].card[z] = id;
  8361. pos |= sd->status.inventory[index].equip;
  8362. found = true;
  8363. break;
  8364. }
  8365. }
  8366. }
  8367. if( !found )
  8368. break;/* we haven't found all the ids for this combo, so we can return */
  8369. }
  8370. aFree(combo_idx);
  8371. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8372. if( j < nb_itemCombo )
  8373. continue;
  8374. /* we got here, means all items in the combo are matching */
  8375. idx = sd->combos.count;
  8376. if( sd->combos.bonus == NULL ) {
  8377. CREATE(sd->combos.bonus, struct script_code *, 1);
  8378. CREATE(sd->combos.id, unsigned short, 1);
  8379. CREATE(sd->combos.pos, unsigned int, 1);
  8380. sd->combos.count = 1;
  8381. } else {
  8382. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8383. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8384. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8385. }
  8386. /* we simply copy the pointer */
  8387. sd->combos.bonus[idx] = data->combos[i]->script;
  8388. /* save this combo's id */
  8389. sd->combos.id[idx] = data->combos[i]->id;
  8390. /* save pos of combo*/
  8391. sd->combos.pos[idx] = pos;
  8392. success++;
  8393. }
  8394. return success;
  8395. }
  8396. /**
  8397. * Called when an item with combo is removed
  8398. * @param *sd
  8399. * @param *data struct item_data
  8400. * @return retval numbers of removed combo
  8401. */
  8402. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8403. int i, retval = 0;
  8404. if( sd->combos.bonus == NULL )
  8405. return 0;/* nothing to do here, player has no combos */
  8406. for( i = 0; i < data->combos_count; i++ ) {
  8407. /* check if this combo exists in this user */
  8408. int x = 0, cursor = 0, j;
  8409. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8410. /* no match, skip this combo */
  8411. if(x >= sd->combos.count)
  8412. continue;
  8413. sd->combos.bonus[x] = NULL;
  8414. sd->combos.id[x] = 0;
  8415. sd->combos.pos[x] = 0;
  8416. retval++;
  8417. /* check if combo requirements still fit */
  8418. if( pc_checkcombo( sd, data ) )
  8419. continue;
  8420. /* move next value to empty slot */
  8421. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8422. if( sd->combos.bonus[j] == NULL )
  8423. continue;
  8424. if( cursor != j ) {
  8425. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8426. sd->combos.id[cursor] = sd->combos.id[j];
  8427. sd->combos.pos[cursor] = sd->combos.pos[j];
  8428. }
  8429. cursor++;
  8430. }
  8431. /* it's empty, we can clear all the memory */
  8432. if( (sd->combos.count = cursor) == 0 ) {
  8433. aFree(sd->combos.bonus);
  8434. aFree(sd->combos.id);
  8435. aFree(sd->combos.pos);
  8436. sd->combos.bonus = NULL;
  8437. sd->combos.id = NULL;
  8438. sd->combos.pos = NULL;
  8439. return retval; /* we also can return at this point for we have no more combos to check */
  8440. }
  8441. }
  8442. return retval;
  8443. }
  8444. /**
  8445. * Load combo data(s) of player
  8446. * @param *sd
  8447. * @return ret numbers of succeed combo
  8448. */
  8449. int pc_load_combo(struct map_session_data *sd) {
  8450. int i, ret = 0;
  8451. for( i = 0; i < EQI_MAX; i++ ) {
  8452. struct item_data *id = NULL;
  8453. short idx = sd->equip_index[i];
  8454. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8455. continue;
  8456. if( id->combos_count )
  8457. ret += pc_checkcombo(sd,id);
  8458. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  8459. struct item_data *data;
  8460. int j;
  8461. for( j = 0; j < id->slot; j++ ) {
  8462. if (!sd->status.inventory[idx].card[j])
  8463. continue;
  8464. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  8465. if( data->combos_count )
  8466. ret += pc_checkcombo(sd,data);
  8467. }
  8468. }
  8469. }
  8470. }
  8471. return ret;
  8472. }
  8473. /*==========================================
  8474. * Equip item on player sd at req_pos from inventory index n
  8475. * return: false - fail; true - success
  8476. *------------------------------------------*/
  8477. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8478. {
  8479. int i, pos, flag = 0, iflag;
  8480. struct item_data *id;
  8481. uint8 res = ITEM_EQUIP_ACK_OK;
  8482. nullpo_retr(false,sd);
  8483. if( n < 0 || n >= MAX_INVENTORY ) {
  8484. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8485. return false;
  8486. }
  8487. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8488. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8489. return false;
  8490. }
  8491. if (!(id = sd->inventory_data[n]))
  8492. return false;
  8493. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8494. if(battle_config.battle_log)
  8495. ShowInfo("equip %hu(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  8496. if((res = pc_isequip(sd,n))) {
  8497. clif_equipitemack(sd,n,0,res); // fail
  8498. return false;
  8499. }
  8500. if (!(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  8501. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8502. return false;
  8503. }
  8504. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8505. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8506. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8507. return false;
  8508. }
  8509. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8510. /** Failing condition:
  8511. * 1. Always failed to equip ammo if no weapon equipped yet
  8512. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8513. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8514. * (4. The rest is relying on item job/class restriction).
  8515. **/
  8516. if (id->type == IT_AMMO) {
  8517. int w_idx = sd->equip_index[EQI_HAND_R];
  8518. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8519. if (w_idx == -1 ||
  8520. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8521. (id->look != A_GRENADE && w_type == W_GRENADE))
  8522. {
  8523. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8524. return false;
  8525. }
  8526. }
  8527. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8528. int a_idx = sd->equip_index[EQI_AMMO];
  8529. if (a_idx != -1) {
  8530. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8531. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8532. (a_type != A_GRENADE && id->look == W_GRENADE))
  8533. {
  8534. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8535. return false;
  8536. }
  8537. }
  8538. }
  8539. }
  8540. if (id->flag.bindOnEquip && !sd->status.inventory[n].bound) {
  8541. sd->status.inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8542. clif_notify_bindOnEquip(sd,n);
  8543. }
  8544. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8545. pos = req_pos&EQP_ACC;
  8546. if (pos == EQP_ACC) //User specified both slots..
  8547. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8548. }
  8549. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8550. pos = req_pos&EQP_SHADOW_ACC;
  8551. if (pos == EQP_SHADOW_ACC)
  8552. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8553. }
  8554. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8555. pos = (req_pos&EQP_ARMS);
  8556. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8557. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8558. }
  8559. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8560. //Update skill-block range database when weapon range changes. [Skotlex]
  8561. i = sd->equip_index[EQI_HAND_R];
  8562. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8563. flag = 1;
  8564. else
  8565. flag = id->range != sd->inventory_data[i]->range;
  8566. }
  8567. for(i=0;i<EQI_MAX;i++) {
  8568. if(pos & equip_bitmask[i]) {
  8569. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8570. pc_unequipitem(sd,sd->equip_index[i],2);
  8571. sd->equip_index[i] = n;
  8572. }
  8573. }
  8574. if(pos==EQP_AMMO) {
  8575. clif_arrowequip(sd,n);
  8576. clif_arrow_fail(sd,3);
  8577. }
  8578. else
  8579. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8580. sd->status.inventory[n].equip=pos;
  8581. if(pos & EQP_HAND_R) {
  8582. if(id)
  8583. sd->weapontype1 = id->look;
  8584. else
  8585. sd->weapontype1 = 0;
  8586. pc_calcweapontype(sd);
  8587. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8588. }
  8589. if(pos & EQP_HAND_L) {
  8590. if(id) {
  8591. if(id->type == IT_WEAPON) {
  8592. sd->status.shield = 0;
  8593. sd->weapontype2 = id->look;
  8594. }
  8595. else
  8596. if(id->type == IT_ARMOR) {
  8597. sd->status.shield = id->look;
  8598. sd->weapontype2 = 0;
  8599. }
  8600. }
  8601. else
  8602. sd->status.shield = sd->weapontype2 = 0;
  8603. pc_calcweapontype(sd);
  8604. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8605. }
  8606. if(pos & EQP_SHOES)
  8607. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8608. pc_set_costume_view(sd);
  8609. pc_checkallowskill(sd); //Check if status changes should be halted.
  8610. iflag = sd->npc_item_flag;
  8611. /* check for combos (MUST be before status_calc_pc) */
  8612. if( id->combos_count )
  8613. pc_checkcombo(sd,id);
  8614. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8615. ; //No cards
  8616. else {
  8617. for( i = 0; i < id->slot; i++ ) {
  8618. struct item_data *data;
  8619. if (!sd->status.inventory[n].card[i])
  8620. continue;
  8621. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8622. if( data->combos_count )
  8623. pc_checkcombo(sd,data);
  8624. }
  8625. }
  8626. }
  8627. status_calc_pc(sd,SCO_NONE);
  8628. if (flag) //Update skill data
  8629. clif_skillinfoblock(sd);
  8630. //OnEquip script [Skotlex]
  8631. if (id) {
  8632. //only run the script if item isn't restricted
  8633. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8634. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8635. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8636. ; //No cards
  8637. else {
  8638. for( i = 0; i < id->slot; i++ ) {
  8639. struct item_data *data;
  8640. if (!sd->status.inventory[n].card[i])
  8641. continue;
  8642. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8643. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8644. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8645. }
  8646. }
  8647. }
  8648. }
  8649. sd->npc_item_flag = iflag;
  8650. return true;
  8651. }
  8652. /*==========================================
  8653. * Called when attemting to unequip an item from player
  8654. * type:
  8655. * 0 - only unequip
  8656. * 1 - calculate status after unequipping
  8657. * 2 - force unequip
  8658. * return: false - fail; true - success
  8659. *------------------------------------------*/
  8660. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8661. int i, iflag;
  8662. bool status_cacl = false;
  8663. nullpo_retr(false,sd);
  8664. if (n < 0 || n >= MAX_INVENTORY) {
  8665. clif_unequipitemack(sd,0,0,0);
  8666. return false;
  8667. }
  8668. if (!sd->status.inventory[n].equip) {
  8669. clif_unequipitemack(sd,n,0,0);
  8670. return false; //Nothing to unequip
  8671. }
  8672. // status change that makes player cannot unequip equipment
  8673. if (!(flag&2) && sd->sc.count &&
  8674. (sd->sc.data[SC_BERSERK] ||
  8675. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8676. sd->sc.data[SC__BLOODYLUST] ||
  8677. sd->sc.data[SC_KYOUGAKU] ||
  8678. (sd->sc.data[SC_PYROCLASTIC] &&
  8679. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8680. {
  8681. clif_unequipitemack(sd,n,0,0);
  8682. return false;
  8683. }
  8684. if (battle_config.battle_log)
  8685. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  8686. for(i = 0; i < EQI_MAX; i++) {
  8687. if (sd->status.inventory[n].equip & equip_bitmask[i])
  8688. sd->equip_index[i] = -1;
  8689. }
  8690. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  8691. sd->weapontype1 = 0;
  8692. sd->status.weapon = sd->weapontype2;
  8693. pc_calcweapontype(sd);
  8694. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8695. if( !battle_config.dancing_weaponswitch_fix )
  8696. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8697. }
  8698. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  8699. sd->status.shield = sd->weapontype2 = 0;
  8700. pc_calcweapontype(sd);
  8701. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8702. }
  8703. if(sd->status.inventory[n].equip & EQP_SHOES)
  8704. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8705. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  8706. pc_set_costume_view(sd);
  8707. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8708. // On weapon change (right and left hand)
  8709. if ((sd->status.inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8710. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8711. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8712. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8713. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8714. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8715. }
  8716. // On armor change
  8717. if (sd->status.inventory[n].equip & EQP_ARMOR) {
  8718. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8719. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8720. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8721. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8722. }
  8723. // On ammo change
  8724. if (sd->inventory_data[n]->type == IT_AMMO)
  8725. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8726. if( sd->state.autobonus&sd->status.inventory[n].equip )
  8727. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  8728. sd->status.inventory[n].equip = 0;
  8729. iflag = sd->npc_item_flag;
  8730. /* check for combos (MUST be before status_calc_pc) */
  8731. if ( sd->inventory_data[n] ) {
  8732. if( sd->inventory_data[n]->combos_count ) {
  8733. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8734. status_cacl = true;
  8735. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8736. ; //No cards
  8737. else {
  8738. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8739. struct item_data *data;
  8740. if (!sd->status.inventory[n].card[i])
  8741. continue;
  8742. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8743. if( data->combos_count ) {
  8744. if( pc_removecombo(sd,data) )
  8745. status_cacl = true;
  8746. }
  8747. }
  8748. }
  8749. }
  8750. }
  8751. if(flag&1 || status_cacl) {
  8752. pc_checkallowskill(sd);
  8753. status_calc_pc(sd,SCO_NONE);
  8754. }
  8755. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8756. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8757. //OnUnEquip script [Skotlex]
  8758. if (sd->inventory_data[n]) {
  8759. if (sd->inventory_data[n]->unequip_script)
  8760. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8761. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8762. ; //No cards
  8763. else {
  8764. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8765. struct item_data *data;
  8766. if (!sd->status.inventory[n].card[i])
  8767. continue;
  8768. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8769. if( data->unequip_script )
  8770. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8771. }
  8772. }
  8773. }
  8774. }
  8775. sd->npc_item_flag = iflag;
  8776. return true;
  8777. }
  8778. /*==========================================
  8779. * Checking if player (sd) has an invalid item
  8780. * and is unequiped on map load (item_noequip)
  8781. *------------------------------------------*/
  8782. void pc_checkitem(struct map_session_data *sd) {
  8783. int i, calc_flag = 0;
  8784. struct item it;
  8785. nullpo_retv(sd);
  8786. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8787. return;
  8788. pc_check_available_item(sd); // Check for invalid(ated) items.
  8789. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8790. it = sd->status.inventory[i];
  8791. if( it.nameid == 0 )
  8792. continue;
  8793. if( !it.equip )
  8794. continue;
  8795. if( it.equip&~pc_equippoint(sd,i) ) {
  8796. pc_unequipitem(sd, i, 2);
  8797. calc_flag = 1;
  8798. continue;
  8799. }
  8800. 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) ) {
  8801. pc_unequipitem(sd, i, 2);
  8802. calc_flag = 1;
  8803. continue;
  8804. }
  8805. }
  8806. if( calc_flag && sd->state.active ) {
  8807. pc_checkallowskill(sd);
  8808. status_calc_pc(sd,SCO_NONE);
  8809. }
  8810. }
  8811. /*==========================================
  8812. * Checks for unavailable items and removes them.
  8813. *------------------------------------------*/
  8814. void pc_check_available_item(struct map_session_data *sd)
  8815. {
  8816. int i;
  8817. unsigned short nameid;
  8818. char output[256];
  8819. nullpo_retv(sd);
  8820. if (battle_config.item_check&0x1) { // Check for invalid(ated) items in inventory.
  8821. for(i = 0; i < MAX_INVENTORY; i++) {
  8822. nameid = sd->status.inventory[i].nameid;
  8823. if (!nameid)
  8824. continue;
  8825. if (!itemdb_available(nameid)) {
  8826. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8827. clif_displaymessage(sd->fd, output);
  8828. ShowWarning("Removed invalid/disabled item id %hu from inventory (amount=%d, char_id=%d).\n", nameid, sd->status.inventory[i].amount, sd->status.char_id);
  8829. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8830. continue;
  8831. }
  8832. if (!sd->status.inventory[i].unique_id && !itemdb_isstackable(nameid))
  8833. sd->status.inventory[i].unique_id = pc_generate_unique_id(sd);
  8834. }
  8835. }
  8836. if (battle_config.item_check&0x2) { // Check for invalid(ated) items in cart.
  8837. for(i = 0; i < MAX_CART; i++) {
  8838. nameid = sd->status.cart[i].nameid;
  8839. if (!nameid)
  8840. continue;
  8841. if (!itemdb_available(nameid)) {
  8842. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  8843. clif_displaymessage(sd->fd, output);
  8844. ShowWarning("Removed invalid/disabled item id %hu from cart (amount=%d, char_id=%d).\n", nameid, sd->status.cart[i].amount, sd->status.char_id);
  8845. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8846. continue;
  8847. }
  8848. if (!sd->status.cart[i].unique_id && !itemdb_isstackable(nameid))
  8849. sd->status.cart[i].unique_id = pc_generate_unique_id(sd);
  8850. }
  8851. }
  8852. if (battle_config.item_check&0x4) { // Check for invalid(ated) items in storage.
  8853. for(i = 0; i < sd->storage_size; i++) {
  8854. nameid = sd->status.storage.items[i].nameid;
  8855. if (!nameid)
  8856. continue;
  8857. if (!itemdb_available(nameid)) {
  8858. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  8859. clif_displaymessage(sd->fd, output);
  8860. ShowWarning("Removed invalid/disabled item id %hu from storage (amount=%d, char_id=%d).\n", nameid, sd->status.storage.items[i].amount, sd->status.char_id);
  8861. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8862. continue;
  8863. }
  8864. if (!sd->status.storage.items[i].unique_id && !itemdb_isstackable(nameid))
  8865. sd->status.storage.items[i].unique_id = pc_generate_unique_id(sd);
  8866. }
  8867. }
  8868. }
  8869. /*==========================================
  8870. * Update PVP rank for sd1 in cmp to sd2
  8871. *------------------------------------------*/
  8872. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8873. {
  8874. struct map_session_data *sd1,*sd2;
  8875. sd1=(struct map_session_data *)bl;
  8876. sd2=va_arg(ap,struct map_session_data *);
  8877. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8878. {// cannot register pvp rank for hidden GMs
  8879. return 0;
  8880. }
  8881. if( sd1->pvp_point > sd2->pvp_point )
  8882. sd2->pvp_rank++;
  8883. return 0;
  8884. }
  8885. /*==========================================
  8886. * Calculate new rank beetween all present players (map_foreachinarea)
  8887. * and display result
  8888. *------------------------------------------*/
  8889. int pc_calc_pvprank(struct map_session_data *sd)
  8890. {
  8891. int old = sd->pvp_rank;
  8892. struct map_data *m = &map[sd->bl.m];
  8893. sd->pvp_rank=1;
  8894. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8895. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8896. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8897. return sd->pvp_rank;
  8898. }
  8899. /*==========================================
  8900. * Calculate next sd ranking calculation from config
  8901. *------------------------------------------*/
  8902. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8903. {
  8904. struct map_session_data *sd;
  8905. sd=map_id2sd(id);
  8906. if(sd==NULL)
  8907. return 0;
  8908. sd->pvp_timer = INVALID_TIMER;
  8909. if( sd->sc.option&OPTION_INVISIBLE )
  8910. {// do not calculate the pvp rank for a hidden GM
  8911. return 0;
  8912. }
  8913. if( pc_calc_pvprank(sd) > 0 )
  8914. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8915. return 0;
  8916. }
  8917. /*==========================================
  8918. * Checking if sd is married
  8919. * Return:
  8920. * partner_id = yes
  8921. * 0 = no
  8922. *------------------------------------------*/
  8923. int pc_ismarried(struct map_session_data *sd)
  8924. {
  8925. if(sd == NULL)
  8926. return -1;
  8927. if(sd->status.partner_id > 0)
  8928. return sd->status.partner_id;
  8929. else
  8930. return 0;
  8931. }
  8932. /*==========================================
  8933. * Marry player sd to player dstsd
  8934. * Return:
  8935. * false = fail
  8936. * true = success
  8937. *------------------------------------------*/
  8938. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  8939. {
  8940. if(sd == NULL || dstsd == NULL ||
  8941. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  8942. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  8943. return false;
  8944. sd->status.partner_id = dstsd->status.char_id;
  8945. dstsd->status.partner_id = sd->status.char_id;
  8946. return true;
  8947. }
  8948. /*==========================================
  8949. * Divorce sd from its partner
  8950. * Return:
  8951. * false = fail
  8952. * true = success
  8953. *------------------------------------------*/
  8954. bool pc_divorce(struct map_session_data *sd)
  8955. {
  8956. struct map_session_data *p_sd;
  8957. int i;
  8958. if( sd == NULL || !pc_ismarried(sd) )
  8959. return false;
  8960. if( !sd->status.partner_id )
  8961. return false; // Char is not married
  8962. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8963. { // Lets char server do the divorce
  8964. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8965. return false; // No char server connected
  8966. return true;
  8967. }
  8968. // Both players online, lets do the divorce manually
  8969. sd->status.partner_id = 0;
  8970. p_sd->status.partner_id = 0;
  8971. for( i = 0; i < MAX_INVENTORY; i++ )
  8972. {
  8973. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8974. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8975. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8976. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8977. }
  8978. clif_divorced(sd, p_sd->status.name);
  8979. clif_divorced(p_sd, sd->status.name);
  8980. return true;
  8981. }
  8982. /**
  8983. * Get the partner map_session_data of a player
  8984. * @param sd : the husband|wife session
  8985. * @return partner session or NULL
  8986. */
  8987. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  8988. if (!sd || !pc_ismarried(sd))
  8989. return NULL;
  8990. return map_charid2sd(sd->status.partner_id);
  8991. }
  8992. /**
  8993. * Get the father map_session_data of a player
  8994. * @param sd : the baby session
  8995. * @return father session or NULL
  8996. */
  8997. struct map_session_data *pc_get_father (struct map_session_data *sd){
  8998. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  8999. return NULL;
  9000. return map_charid2sd(sd->status.father);
  9001. }
  9002. /**
  9003. * Get the mother map_session_data of a player
  9004. * @param sd : the baby session
  9005. * @return mother session or NULL
  9006. */
  9007. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9008. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9009. return NULL;
  9010. return map_charid2sd(sd->status.mother);
  9011. }
  9012. /*==========================================
  9013. * Get sd children charid. (Need to be married)
  9014. *------------------------------------------*/
  9015. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9016. {
  9017. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9018. // charid2sd returns NULL if not found
  9019. return NULL;
  9020. return map_charid2sd(sd->status.child);
  9021. }
  9022. /*==========================================
  9023. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9024. *------------------------------------------*/
  9025. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9026. {
  9027. int hp = 0, sp = 0;
  9028. if( pc_isdead(sd) )
  9029. return;
  9030. if (sd->hp_loss.value) {
  9031. sd->hp_loss.tick += diff_tick;
  9032. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9033. hp += sd->hp_loss.value;
  9034. sd->hp_loss.tick -= sd->hp_loss.rate;
  9035. }
  9036. if(hp >= sd->battle_status.hp)
  9037. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9038. }
  9039. if (sd->sp_loss.value) {
  9040. sd->sp_loss.tick += diff_tick;
  9041. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9042. sp += sd->sp_loss.value;
  9043. sd->sp_loss.tick -= sd->sp_loss.rate;
  9044. }
  9045. }
  9046. if (hp > 0 || sp > 0)
  9047. status_zap(&sd->bl, hp, sp);
  9048. }
  9049. //Character regen. Flag is used to know which types of regen can take place.
  9050. //&1: HP regen
  9051. //&2: SP regen
  9052. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9053. {
  9054. int hp = 0, sp = 0;
  9055. if (sd->hp_regen.value) {
  9056. sd->hp_regen.tick += diff_tick;
  9057. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9058. hp += sd->hp_regen.value;
  9059. sd->hp_regen.tick -= sd->hp_regen.rate;
  9060. }
  9061. }
  9062. if (sd->sp_regen.value) {
  9063. sd->sp_regen.tick += diff_tick;
  9064. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9065. sp += sd->sp_regen.value;
  9066. sd->sp_regen.tick -= sd->sp_regen.rate;
  9067. }
  9068. }
  9069. if (sd->percent_hp_regen.value) {
  9070. sd->percent_hp_regen.tick += diff_tick;
  9071. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9072. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9073. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9074. }
  9075. }
  9076. if (sd->percent_sp_regen.value) {
  9077. sd->percent_sp_regen.tick += diff_tick;
  9078. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9079. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9080. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9081. }
  9082. }
  9083. if (hp > 0 || sp > 0)
  9084. status_heal(&sd->bl, hp, sp, 0);
  9085. }
  9086. /*==========================================
  9087. * Memo player sd savepoint. (map,x,y)
  9088. *------------------------------------------*/
  9089. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9090. {
  9091. nullpo_retv(sd);
  9092. sd->status.save_point.map = mapindex;
  9093. sd->status.save_point.x = x;
  9094. sd->status.save_point.y = y;
  9095. }
  9096. /*==========================================
  9097. * Save 1 player data at autosave interval
  9098. *------------------------------------------*/
  9099. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9100. {
  9101. int interval;
  9102. struct s_mapiterator* iter;
  9103. struct map_session_data* sd;
  9104. static int last_save_id = 0, save_flag = 0;
  9105. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9106. save_flag = 0;
  9107. else
  9108. save_flag = 1; //Noone was saved, so save first found char.
  9109. iter = mapit_getallusers();
  9110. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9111. {
  9112. if(sd->bl.id == last_save_id && save_flag != 1) {
  9113. save_flag = 1;
  9114. continue;
  9115. }
  9116. if(save_flag != 1) //Not our turn to save yet.
  9117. continue;
  9118. //Save char.
  9119. last_save_id = sd->bl.id;
  9120. save_flag = 2;
  9121. if (pc_isvip(sd)) // Check if we're still VIP
  9122. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9123. chrif_save(sd,0);
  9124. break;
  9125. }
  9126. mapit_free(iter);
  9127. interval = autosave_interval/(map_usercount()+1);
  9128. if(interval < minsave_interval)
  9129. interval = minsave_interval;
  9130. add_timer(gettick()+interval,pc_autosave,0,0);
  9131. return 0;
  9132. }
  9133. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9134. {
  9135. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9136. { //Night/day state does not match.
  9137. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  9138. sd->state.night = night_flag;
  9139. return 1;
  9140. }
  9141. return 0;
  9142. }
  9143. /*================================================
  9144. * timer to do the day [Yor]
  9145. * data: 0 = called by timer, 1 = gmcommand/script
  9146. *------------------------------------------------*/
  9147. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9148. {
  9149. char tmp_soutput[1024];
  9150. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9151. return 0;
  9152. if (!night_flag)
  9153. return 0; //Already day.
  9154. night_flag = 0; // 0=day, 1=night [Yor]
  9155. map_foreachpc(pc_daynight_timer_sub);
  9156. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9157. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9158. return 0;
  9159. }
  9160. /*================================================
  9161. * timer to do the night [Yor]
  9162. * data: 0 = called by timer, 1 = gmcommand/script
  9163. *------------------------------------------------*/
  9164. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9165. {
  9166. char tmp_soutput[1024];
  9167. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9168. return 0;
  9169. if (night_flag)
  9170. return 0; //Already nigth.
  9171. night_flag = 1; // 0=day, 1=night [Yor]
  9172. map_foreachpc(pc_daynight_timer_sub);
  9173. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9174. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9175. return 0;
  9176. }
  9177. /**
  9178. * Attempt to stand up a player
  9179. * @param sd
  9180. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9181. * @return True if success, Fals if failed
  9182. */
  9183. bool pc_setstand(struct map_session_data *sd, bool force){
  9184. nullpo_ret(sd);
  9185. // Cannot stand yet
  9186. // TODO: Move to SCS_NOSTAND [Cydh]
  9187. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9188. return false;
  9189. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9190. clif_status_load(&sd->bl,SI_SIT,0);
  9191. clif_standing(&sd->bl); //Inform area PC is standing
  9192. //Reset sitting tick.
  9193. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9194. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9195. return true;
  9196. }
  9197. /**
  9198. * Mechanic (MADO GEAR)
  9199. **/
  9200. void pc_overheat(struct map_session_data *sd, int val) {
  9201. int heat = val, skill,
  9202. limit[] = { 10, 20, 28, 46, 66 };
  9203. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9204. return; // already burning
  9205. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9206. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9207. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9208. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9209. }
  9210. heat = max(0,heat); // Avoid negative HEAT
  9211. if( heat >= limit[skill] )
  9212. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9213. else
  9214. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9215. return;
  9216. }
  9217. /**
  9218. * Check if player is autolooting given itemID.
  9219. */
  9220. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9221. {
  9222. uint8 i = 0;
  9223. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9224. return true;
  9225. if (!sd->state.autolooting)
  9226. return false;
  9227. if (sd->state.autolooting)
  9228. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9229. return (i != AUTOLOOTITEM_SIZE);
  9230. }
  9231. /**
  9232. * Checks if player can use @/#command
  9233. * @param sd Player map session data
  9234. * @param command Command name without @/# and params
  9235. * @param type is it atcommand or charcommand
  9236. */
  9237. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9238. {
  9239. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9240. }
  9241. /**
  9242. * Checks if commands used by a player should be logged
  9243. * according to their group setting.
  9244. * @param sd Player map session data
  9245. */
  9246. bool pc_should_log_commands(struct map_session_data *sd)
  9247. {
  9248. return pc_group_should_log_commands(pc_get_group_id(sd));
  9249. }
  9250. /**
  9251. * Spirit Charm expiration timer.
  9252. * @see TimerFunc
  9253. */
  9254. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9255. {
  9256. struct map_session_data *sd;
  9257. int i;
  9258. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9259. return 1;
  9260. if (sd->spiritcharm <= 0) {
  9261. 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);
  9262. sd->spiritcharm = 0;
  9263. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9264. return 0;
  9265. }
  9266. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9267. if (i == sd->spiritcharm) {
  9268. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9269. return 0;
  9270. }
  9271. sd->spiritcharm--;
  9272. if (i != sd->spiritcharm)
  9273. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9274. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9275. if (sd->spiritcharm <= 0)
  9276. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9277. clif_spiritcharm(sd);
  9278. return 0;
  9279. }
  9280. /**
  9281. * Adds a spirit charm.
  9282. * @param sd: Target character
  9283. * @param interval: Duration
  9284. * @param max: Maximum amount of charms to add
  9285. * @param type: Charm type (@see spirit_charm_types)
  9286. */
  9287. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9288. {
  9289. int tid, i;
  9290. nullpo_retv(sd);
  9291. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9292. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9293. if (max > MAX_SPIRITCHARM)
  9294. max = MAX_SPIRITCHARM;
  9295. if (sd->spiritcharm < 0)
  9296. sd->spiritcharm = 0;
  9297. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9298. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9299. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9300. sd->spiritcharm--;
  9301. if (sd->spiritcharm != 0)
  9302. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9303. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9304. }
  9305. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9306. 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);
  9307. if (i != sd->spiritcharm)
  9308. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9309. sd->spiritcharm_timer[i] = tid;
  9310. sd->spiritcharm++;
  9311. sd->spiritcharm_type = type;
  9312. clif_spiritcharm(sd);
  9313. }
  9314. /**
  9315. * Removes one or more spirit charms.
  9316. * @param sd: The target character
  9317. * @param count: Amount of charms to remove
  9318. * @param type: Type of charm to remove
  9319. */
  9320. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9321. {
  9322. int i;
  9323. nullpo_retv(sd);
  9324. if (sd->spiritcharm_type != type)
  9325. return;
  9326. if (sd->spiritcharm <= 0) {
  9327. sd->spiritcharm = 0;
  9328. return;
  9329. }
  9330. if (count <= 0)
  9331. return;
  9332. if (count > sd->spiritcharm)
  9333. count = sd->spiritcharm;
  9334. sd->spiritcharm -= count;
  9335. if (count > MAX_SPIRITCHARM)
  9336. count = MAX_SPIRITCHARM;
  9337. for (i = 0; i < count; i++) {
  9338. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9339. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9340. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9341. }
  9342. }
  9343. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9344. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9345. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9346. }
  9347. if (sd->spiritcharm <= 0)
  9348. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9349. clif_spiritcharm(sd);
  9350. }
  9351. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9352. /**
  9353. * Renewal EXP/Item Drop rate modifier based on level penalty
  9354. * @param level_diff: Monster and Player level difference
  9355. * @param mob_class: Monster class
  9356. * @param mode: Monster mode
  9357. * @param type: 1 - EXP, 2 - Item Drop
  9358. * @return Penalty rate
  9359. */
  9360. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9361. {
  9362. int rate = 100;
  9363. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9364. return rate;
  9365. if (level_diff < 0)
  9366. level_diff = MAX_LEVEL + (~level_diff + 1);
  9367. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9368. return rate;
  9369. return 100; // Penalty not found, return default
  9370. }
  9371. #endif
  9372. int pc_split_str(char *str,char **val,int num)
  9373. {
  9374. int i;
  9375. for (i=0; i<num && str; i++){
  9376. val[i] = str;
  9377. str = strchr(str,',');
  9378. if (str && i<num-1) //Do not remove a trailing comma.
  9379. *str++=0;
  9380. }
  9381. return i;
  9382. }
  9383. int pc_split_atoi(char* str, int* val, char sep, int max)
  9384. {
  9385. int i,j;
  9386. for (i=0; i<max; i++) {
  9387. if (!str) break;
  9388. val[i] = atoi(str);
  9389. str = strchr(str,sep);
  9390. if (str)
  9391. *str++=0;
  9392. }
  9393. //Zero up the remaining.
  9394. for(j=i; j < max; j++)
  9395. val[j] = 0;
  9396. return i;
  9397. }
  9398. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9399. {
  9400. static int warning=0;
  9401. int i,j;
  9402. for (i=0; i<max; i++) {
  9403. double f;
  9404. if (!str) break;
  9405. f = atof(str);
  9406. if (f < 0)
  9407. val[i] = 0;
  9408. else if (f > UINT_MAX) {
  9409. val[i] = UINT_MAX;
  9410. if (!warning) {
  9411. warning = 1;
  9412. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9413. }
  9414. } else
  9415. val[i] = (unsigned int)f;
  9416. str = strchr(str,sep);
  9417. if (str)
  9418. *str++=0;
  9419. }
  9420. //Zero up the remaining.
  9421. for(j=i; j < max; j++)
  9422. val[j] = 0;
  9423. return i;
  9424. }
  9425. /*==========================================
  9426. * sub DB reading.
  9427. * Function used to read skill_tree.txt
  9428. *------------------------------------------*/
  9429. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9430. {
  9431. uint32 baselv, joblv, baselv_max, joblv_max;
  9432. uint16 skill_id, skill_lv, skill_lv_max;
  9433. int idx, class_;
  9434. unsigned int i, offset, skill_idx;
  9435. class_ = atoi(fields[0]);
  9436. skill_id = (uint16)atoi(fields[1]);
  9437. skill_lv = (uint16)atoi(fields[2]);
  9438. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9439. baselv = (uint32)atoi(fields[3]);
  9440. joblv = (uint32)atoi(fields[4]);
  9441. offset = 5;
  9442. } else {
  9443. baselv = joblv = 0;
  9444. offset = 3;
  9445. }
  9446. if(!pcdb_checkid(class_))
  9447. {
  9448. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9449. return false;
  9450. }
  9451. idx = pc_class2idx(class_);
  9452. if (!skill_get_index(skill_id)) {
  9453. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9454. return false;
  9455. }
  9456. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9457. 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);
  9458. skill_lv = skill_lv_max;
  9459. }
  9460. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9461. 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);
  9462. baselv = baselv_max;
  9463. }
  9464. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9465. 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);
  9466. joblv = joblv_max;
  9467. }
  9468. //This is to avoid adding two lines for the same skill. [Skotlex]
  9469. 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 );
  9470. if( skill_idx == MAX_SKILL_TREE )
  9471. {
  9472. 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_);
  9473. return false;
  9474. }
  9475. else if(skill_tree[idx][skill_idx].skill_id)
  9476. {
  9477. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9478. }
  9479. skill_tree[idx][skill_idx].skill_id = skill_id;
  9480. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9481. skill_tree[idx][skill_idx].baselv = baselv;
  9482. skill_tree[idx][skill_idx].joblv = joblv;
  9483. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9484. {
  9485. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9486. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9487. if (skill_id == 0)
  9488. continue;
  9489. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9490. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9491. return false;
  9492. }
  9493. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9494. 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);
  9495. skill_lv = skill_lv_max;
  9496. }
  9497. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9498. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9499. }
  9500. return true;
  9501. }
  9502. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9503. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9504. {
  9505. int type, class_, diff;
  9506. type = atoi(fields[0]); //1=experience, 2=item drop
  9507. class_ = atoi(fields[1]);
  9508. diff = atoi(fields[2]);
  9509. if( type != 1 && type != 2 ){
  9510. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9511. return false;
  9512. }
  9513. if( !CHK_CLASS(class_) ){
  9514. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9515. return false;
  9516. }
  9517. diff = min(diff, MAX_LEVEL);
  9518. if( diff < 0 )
  9519. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9520. level_penalty[type][class_][diff] = atoi(fields[3]);
  9521. return true;
  9522. }
  9523. #endif
  9524. /** [Cydh]
  9525. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9526. * @param level Base level of player
  9527. * @param class_ Job ID @see enum e_job
  9528. * @return base_hp
  9529. */
  9530. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9531. double base_hp;
  9532. uint16 i, idx = pc_class2idx(class_);
  9533. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9534. #ifndef RENEWAL
  9535. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9536. #endif
  9537. for (i = 2; i <= level; i++)
  9538. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9539. if (class_ == JOB_SUMMONER)
  9540. base_hp += floor((base_hp / 2) + 0.5);
  9541. return (unsigned int)base_hp;
  9542. }
  9543. /** [Playtester]
  9544. * Calculates base sp of player.
  9545. * @param level Base level of player
  9546. * @param class_ Job ID @see enum e_job
  9547. * @return base_sp
  9548. */
  9549. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9550. double base_sp;
  9551. uint16 idx = pc_class2idx(class_);
  9552. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9553. switch (class_) {
  9554. case JOB_NINJA:
  9555. if (level >= 10)
  9556. base_sp -= 22;
  9557. else
  9558. base_sp = 11 + 3*level;
  9559. break;
  9560. case JOB_GUNSLINGER:
  9561. if (level > 10)
  9562. base_sp -= 18;
  9563. else
  9564. base_sp = 9 + 3*level;
  9565. break;
  9566. case JOB_SUMMONER:
  9567. base_sp -= floor(base_sp / 2);
  9568. break;
  9569. }
  9570. return (unsigned int)base_sp;
  9571. }
  9572. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9573. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9574. int idx, class_;
  9575. unsigned int i;
  9576. class_ = atoi(fields[0]);
  9577. if (!pcdb_checkid(class_)) {
  9578. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9579. return false;
  9580. }
  9581. idx = pc_class2idx(class_);
  9582. job_info[idx].max_weight_base = atoi(fields[1]);
  9583. job_info[idx].hp_factor = atoi(fields[2]);
  9584. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9585. job_info[idx].sp_factor = atoi(fields[4]);
  9586. #ifdef RENEWAL_ASPD
  9587. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9588. #else
  9589. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9590. #endif
  9591. {
  9592. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9593. }
  9594. return true;
  9595. }
  9596. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9597. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9598. {
  9599. int idx, class_, i;
  9600. class_ = atoi(fields[0]);
  9601. if(!pcdb_checkid(class_))
  9602. {
  9603. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9604. return false;
  9605. }
  9606. idx = pc_class2idx(class_);
  9607. for(i = 1; i < columns; i++)
  9608. {
  9609. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9610. }
  9611. return true;
  9612. }
  9613. //Reading job_exp.txt line
  9614. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9615. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9616. {
  9617. int idx, i, type;
  9618. int job_id,job_count,jobs[CLASS_COUNT];
  9619. unsigned int ui, maxlvl;
  9620. maxlvl = atoi(fields[0]);
  9621. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9622. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9623. return false;
  9624. }
  9625. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9626. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9627. return false;
  9628. }
  9629. type = atoi(fields[2]);
  9630. if(type < 0 || type > 1){
  9631. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9632. return false;
  9633. }
  9634. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9635. if (job_count < 1)
  9636. return false;
  9637. job_id = jobs[0];
  9638. if(!pcdb_checkid(job_id)){
  9639. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9640. return false;
  9641. }
  9642. idx = pc_class2idx(job_id);
  9643. job_info[idx].max_level[type] = maxlvl;
  9644. for(i=0; i<maxlvl; i++)
  9645. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9646. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9647. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9648. //should look like this:
  9649. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9650. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9651. job_info[idx].max_level[type]--;
  9652. if (job_info[idx].max_level[type] < maxlvl) {
  9653. 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]);
  9654. ShowInfo("Filling the missing values with the last exp entry.\n");
  9655. //Fill the requested values with the last entry.
  9656. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9657. for (; ui+2 < maxlvl; ui++)
  9658. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9659. job_info[idx].max_level[type] = maxlvl;
  9660. }
  9661. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9662. for (i = 1; i < job_count; i++) {
  9663. job_id = jobs[i];
  9664. if (!pcdb_checkid(job_id)) {
  9665. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9666. continue;
  9667. }
  9668. idx = pc_class2idx(job_id);
  9669. 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]));
  9670. job_info[idx].max_level[type] = maxlvl;
  9671. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9672. }
  9673. return true;
  9674. }
  9675. /**
  9676. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9677. * startlvl,endlvl,class,type,values...
  9678. */
  9679. #ifdef HP_SP_TABLES
  9680. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9681. {
  9682. int i, startlvl, endlvl;
  9683. int job_count,jobs[CLASS_COUNT];
  9684. short type;
  9685. startlvl = atoi(fields[0]);
  9686. if(startlvl<1){
  9687. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9688. return false;
  9689. }
  9690. endlvl = atoi(fields[1]);
  9691. if(endlvl<1 || endlvl<startlvl){
  9692. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9693. return false;
  9694. }
  9695. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9696. 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);
  9697. return false;
  9698. }
  9699. type = atoi(fields[3]);
  9700. if(type < 0 || type > 1){
  9701. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9702. return false;
  9703. }
  9704. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9705. if (job_count < 1)
  9706. return false;
  9707. for (i = 0; i < job_count; i++) {
  9708. int idx, job_id = jobs[i], use_endlvl;
  9709. if (!pcdb_checkid(job_id)) {
  9710. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9711. return false;
  9712. }
  9713. idx = pc_class2idx(job_id);
  9714. if (startlvl > job_info[idx].max_level[0]) {
  9715. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9716. return false;
  9717. }
  9718. //Just read until available max level for this job, don't use MAX_LEVEL!
  9719. use_endlvl = endlvl;
  9720. if (use_endlvl > job_info[idx].max_level[0])
  9721. use_endlvl = job_info[idx].max_level[0];
  9722. if(type == 0) { //hp type
  9723. uint16 j;
  9724. for(j = 0; j < use_endlvl; j++) {
  9725. if (atoi(fields[j+4])) {
  9726. uint16 lvl_idx = startlvl-1+j;
  9727. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9728. //Tells if this HP is lower than previous level (but not for 99->100)
  9729. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9730. 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",
  9731. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9732. }
  9733. }
  9734. }
  9735. else { //sp type
  9736. uint16 j;
  9737. for(j = 0; j < use_endlvl; j++) {
  9738. if (atoi(fields[j+4])) {
  9739. uint16 lvl_idx = startlvl-1+j;
  9740. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9741. //Tells if this SP is lower than previous level (but not for 99->100)
  9742. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9743. 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",
  9744. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9745. }
  9746. }
  9747. }
  9748. }
  9749. return true;
  9750. }
  9751. #endif
  9752. /** [Cydh]
  9753. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9754. */
  9755. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9756. {
  9757. int idx, class_;
  9758. uint16 str, agi, vit, int_, dex, luk;
  9759. script_get_constant(trim(fields[0]),&class_);
  9760. if ((idx = pc_class2idx(class_)) < 0) {
  9761. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9762. return false;
  9763. }
  9764. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9765. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9766. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9767. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9768. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9769. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9770. job_info[idx].max_param.str = str;
  9771. job_info[idx].max_param.agi = agi;
  9772. job_info[idx].max_param.vit = vit;
  9773. job_info[idx].max_param.int_ = int_;
  9774. job_info[idx].max_param.dex = dex;
  9775. job_info[idx].max_param.luk = luk;
  9776. return true;
  9777. }
  9778. /**
  9779. * Read job_noenter_map.txt
  9780. **/
  9781. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  9782. int idx, class_ = -1;
  9783. if (ISDIGIT(str[0][0])) {
  9784. class_ = atoi(str[0]);
  9785. } else {
  9786. if (!script_get_constant(str[0], &class_)) {
  9787. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  9788. return false;
  9789. }
  9790. }
  9791. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  9792. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  9793. return false;
  9794. }
  9795. job_info[idx].noenter_map.zone = atoi(str[1]);
  9796. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  9797. return true;
  9798. }
  9799. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9800. int i=1;
  9801. char line[24000]; //FIXME this seem too big
  9802. FILE *fp;
  9803. sprintf(line, "%s/statpoint.txt", basedir);
  9804. fp=fopen(line,"r");
  9805. if(fp == NULL){
  9806. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9807. return max(last_s,i);
  9808. } else {
  9809. int entries=0;
  9810. while(fgets(line, sizeof(line), fp))
  9811. {
  9812. int stat;
  9813. trim(line);
  9814. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9815. continue;
  9816. if ((stat=strtoul(line,NULL,10))<0)
  9817. stat=0;
  9818. if (i > MAX_LEVEL)
  9819. break;
  9820. statp[i]=stat;
  9821. i++;
  9822. entries++;
  9823. }
  9824. fclose(fp);
  9825. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9826. }
  9827. return max(last_s,i);
  9828. }
  9829. /*==========================================
  9830. * pc DB reading.
  9831. * job_exp.txt - required experience values
  9832. * skill_tree.txt - skill tree for every class
  9833. * attr_fix.txt - elemental adjustment table
  9834. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9835. * job_db2.txt - job,stats bonuses/lvl
  9836. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9837. *------------------------------------------*/
  9838. void pc_readdb(void) {
  9839. int i, k, s = 1;
  9840. const char* dbsubpath[] = {
  9841. "",
  9842. "/"DBIMPORT,
  9843. //add other path here
  9844. };
  9845. //reset
  9846. memset(job_info,0,sizeof(job_info)); // job_info table
  9847. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9848. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9849. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9850. for( k=1; k < 3; k++ ){ // fill in the blanks
  9851. int j;
  9852. for( j = 0; j < CLASS_ALL; j++ ){
  9853. int tmp = 0;
  9854. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9855. if( i == MAX_LEVEL+1 )
  9856. tmp = level_penalty[k][j][0];// reset
  9857. if( level_penalty[k][j][i] > 0 )
  9858. tmp = level_penalty[k][j][i];
  9859. else
  9860. level_penalty[k][j][i] = tmp;
  9861. }
  9862. }
  9863. }
  9864. #endif
  9865. // reset then read statspoint
  9866. memset(statp,0,sizeof(statp));
  9867. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9868. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  9869. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  9870. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9871. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9872. if(i==0) {
  9873. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9874. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9875. }
  9876. else {
  9877. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9878. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9879. }
  9880. s = pc_read_statsdb(dbsubpath2,s,i);
  9881. if (i == 0)
  9882. #ifdef RENEWAL_ASPD
  9883. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9884. #else
  9885. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9886. #endif
  9887. else
  9888. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9889. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  9890. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  9891. #ifdef HP_SP_TABLES
  9892. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  9893. #endif
  9894. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  9895. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i);
  9896. aFree(dbsubpath1);
  9897. aFree(dbsubpath2);
  9898. }
  9899. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  9900. memset(skill_tree, 0, sizeof(skill_tree));
  9901. 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);
  9902. 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);
  9903. // generate the remaining parts of the db if necessary
  9904. k = battle_config.use_statpoint_table; //save setting
  9905. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  9906. statp[0] = 45; // seed value
  9907. for (; s <= MAX_LEVEL; s++)
  9908. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  9909. battle_config.use_statpoint_table = k; //restore setting
  9910. //Checking if all class have their data
  9911. for (i = 0; i < JOB_MAX; i++) {
  9912. int idx;
  9913. uint16 j;
  9914. if (!pcdb_checkid(i))
  9915. continue;
  9916. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  9917. continue; //Classes that do not need exp tables.
  9918. idx = pc_class2idx(i);
  9919. if (!job_info[idx].max_level[0])
  9920. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  9921. if (!job_info[idx].max_level[1])
  9922. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  9923. //Init and checking the empty value of Base HP/SP [Cydh]
  9924. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  9925. if (job_info[idx].base_hp[j] == 0)
  9926. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  9927. if (job_info[idx].base_sp[j] == 0)
  9928. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  9929. }
  9930. }
  9931. }
  9932. // Read MOTD on startup. [Valaris]
  9933. int pc_read_motd(void)
  9934. {
  9935. FILE* fp;
  9936. // clear old MOTD
  9937. memset(motd_text, 0, sizeof(motd_text));
  9938. // read current MOTD
  9939. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  9940. {
  9941. unsigned int entries = 0;
  9942. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  9943. {
  9944. char* buf = motd_text[entries];
  9945. unsigned int lines = 0;
  9946. size_t len;
  9947. lines++;
  9948. if( buf[0] == '/' && buf[1] == '/' )
  9949. continue;
  9950. len = strlen(buf);
  9951. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  9952. len--;
  9953. if( len ) {
  9954. char * ptr;
  9955. buf[len] = 0;
  9956. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  9957. 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);
  9958. }
  9959. else {// empty line
  9960. buf[0] = ' ';
  9961. buf[1] = 0;
  9962. }
  9963. entries++;
  9964. }
  9965. fclose(fp);
  9966. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  9967. }
  9968. else
  9969. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  9970. return 0;
  9971. }
  9972. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  9973. int i,cursor = 0;
  9974. struct item_cd* cd = NULL;
  9975. if( load ) {
  9976. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  9977. // no item cooldown is associated with this character
  9978. return;
  9979. }
  9980. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9981. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  9982. sd->item_delay[cursor].tick = cd->tick[i];
  9983. sd->item_delay[cursor].nameid = cd->nameid[i];
  9984. cursor++;
  9985. }
  9986. }
  9987. idb_remove(itemcd_db,sd->status.char_id);
  9988. } else {
  9989. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  9990. // create a new skill cooldown object for map storage
  9991. CREATE( cd, struct item_cd, 1 );
  9992. idb_put( itemcd_db, sd->status.char_id, cd );
  9993. }
  9994. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9995. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  9996. cd->tick[cursor] = sd->item_delay[i].tick;
  9997. cd->nameid[cursor] = sd->item_delay[i].nameid;
  9998. cursor++;
  9999. }
  10000. }
  10001. }
  10002. return;
  10003. }
  10004. /**
  10005. * Add item delay to player's item delay data
  10006. * @param sd Player
  10007. * @param id Item data
  10008. * @param tick Current tick
  10009. * @param n Item index in inventory
  10010. * @return 0: No delay, can consume item.
  10011. * 1: Has delay, cancel consumption.
  10012. **/
  10013. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10014. int i;
  10015. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10016. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10017. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10018. if( i < MAX_ITEMDELAYS ) {
  10019. if( sd->item_delay[i].nameid ) {// found
  10020. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10021. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10022. char e_msg[CHAT_SIZE_MAX];
  10023. if( e_tick > 99 )
  10024. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10025. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10026. else
  10027. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10028. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10029. clif_colormes(sd->fd,color_table[COLOR_YELLOW],e_msg);
  10030. return 1; // Delay has not expired yet
  10031. }
  10032. } else {// not yet used item (all slots are initially empty)
  10033. sd->item_delay[i].nameid = id->nameid;
  10034. }
  10035. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10036. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10037. } else {// should not happen
  10038. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10039. }
  10040. //clean up used delays so we can give room for more
  10041. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10042. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10043. sd->item_delay[i].tick = 0;
  10044. sd->item_delay[i].nameid = 0;
  10045. }
  10046. }
  10047. return 0;
  10048. }
  10049. /**
  10050. * Check if player has delay to reuse item
  10051. * @param sd Player
  10052. * @param id Item data
  10053. * @param tick Current tick
  10054. * @param n Item index in inventory
  10055. * @return 0: No delay, can consume item.
  10056. * 1: Has delay, cancel consumption.
  10057. **/
  10058. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10059. struct status_change *sc = NULL;
  10060. nullpo_retr(0, sd);
  10061. nullpo_retr(0, id);
  10062. // Do normal delay assignment
  10063. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10064. return pc_itemcd_add(sd, id, tick, n);
  10065. // Send reply of delay remains
  10066. if (sc->data[id->delay_sc]) {
  10067. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10068. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10069. return 1;
  10070. }
  10071. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10072. return 0;
  10073. }
  10074. /**
  10075. * Clear the dmglog data from player
  10076. * @param sd
  10077. * @param md
  10078. **/
  10079. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10080. {
  10081. uint8 i;
  10082. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10083. if (i < DAMAGELOG_SIZE) {
  10084. md->dmglog[i].id = 0;
  10085. md->dmglog[i].dmg = 0;
  10086. md->dmglog[i].flag = 0;
  10087. }
  10088. }
  10089. /**
  10090. * Add log to player's dmglog
  10091. * @param sd
  10092. * @param id Monster's GID
  10093. **/
  10094. void pc_damage_log_add(struct map_session_data *sd, int id)
  10095. {
  10096. uint8 i = 0;
  10097. if (!sd || !id)
  10098. return;
  10099. //Only store new data, don't need to renew the old one with same id
  10100. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10101. if (i < DAMAGELOG_SIZE_PC)
  10102. return;
  10103. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10104. if (sd->dmglog[i] == 0) {
  10105. sd->dmglog[i] = id;
  10106. return;
  10107. }
  10108. }
  10109. }
  10110. /**
  10111. * Clear dmglog data from player
  10112. * @param sd
  10113. * @param id Monster's id
  10114. **/
  10115. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10116. {
  10117. uint8 i;
  10118. struct mob_data *md = NULL;
  10119. if (!sd)
  10120. return;
  10121. if (!id) {
  10122. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10123. if( !sd->dmglog[i] ) //skip the empty value
  10124. continue;
  10125. if ((md = map_id2md(sd->dmglog[i])))
  10126. pc_clear_log_damage_sub(sd->status.char_id,md);
  10127. sd->dmglog[i] = 0;
  10128. }
  10129. }
  10130. else {
  10131. if ((md = map_id2md(id)))
  10132. pc_clear_log_damage_sub(sd->status.char_id,md);
  10133. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10134. if (i < DAMAGELOG_SIZE_PC)
  10135. sd->dmglog[i] = 0;
  10136. }
  10137. }
  10138. /**
  10139. * Status change data arrived from char-server
  10140. * @param sd: Player data
  10141. */
  10142. void pc_scdata_received(struct map_session_data *sd) {
  10143. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10144. }
  10145. /**
  10146. * Check player account expiration time and rental item expirations
  10147. * @param sd: Player data
  10148. */
  10149. void pc_check_expiration(struct map_session_data *sd) {
  10150. #ifndef ENABLE_SC_SAVING
  10151. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10152. #endif
  10153. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10154. time_t exp_time = sd->expiration_time;
  10155. char tmpstr[1024];
  10156. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10157. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  10158. pc_expire_check(sd);
  10159. }
  10160. }
  10161. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10162. struct map_session_data *sd = map_id2sd(id);
  10163. if( !sd ) return 0;
  10164. sd->expiration_tid = INVALID_TIMER;
  10165. if( sd->fd )
  10166. clif_authfail_fd(sd->fd,10);
  10167. map_quit(sd);
  10168. return 0;
  10169. }
  10170. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10171. struct map_session_data *sd = map_id2sd(id);
  10172. if (!sd)
  10173. return 0;
  10174. sd->autotrade_tid = INVALID_TIMER;
  10175. if (sd->state.autotrade&2)
  10176. vending_reopen(sd);
  10177. if (sd->state.autotrade&4)
  10178. buyingstore_reopen(sd);
  10179. if (!sd->vender_id && !sd->buyer_id) {
  10180. sd->state.autotrade = 0;
  10181. map_quit(sd);
  10182. }
  10183. return 0;
  10184. }
  10185. /* this timer exists only when a character with a expire timer > 24h is online */
  10186. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10187. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10188. struct s_mapiterator* iter;
  10189. struct map_session_data* sd;
  10190. iter = mapit_getallusers();
  10191. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10192. if( sd->expiration_time )
  10193. pc_expire_check(sd);
  10194. mapit_free(iter);
  10195. return 0;
  10196. }
  10197. void pc_expire_check(struct map_session_data *sd) {
  10198. /* ongoing timer */
  10199. if( sd->expiration_tid != INVALID_TIMER )
  10200. return;
  10201. /* not within the next 24h, enable the global check */
  10202. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10203. /* global check not running, enable */
  10204. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10205. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10206. return;
  10207. }
  10208. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10209. }
  10210. /**
  10211. * Deposit some money to bank
  10212. * @param sd
  10213. * @param money Amount of money to deposit
  10214. **/
  10215. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10216. unsigned int limit_check = money + sd->bank_vault;
  10217. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10218. return BDA_OVERFLOW;
  10219. } else if ( money > sd->status.zeny ) {
  10220. return BDA_NO_MONEY;
  10221. }
  10222. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10223. return BDA_NO_MONEY;
  10224. sd->bank_vault += money;
  10225. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10226. if( save_settings&CHARSAVE_BANK )
  10227. chrif_save(sd,0);
  10228. return BDA_SUCCESS;
  10229. }
  10230. /**
  10231. * Withdraw money from bank
  10232. * @param sd
  10233. * @param money Amount of money that will be withdrawn
  10234. **/
  10235. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10236. unsigned int limit_check = money + sd->status.zeny;
  10237. if( money <= 0 ) {
  10238. return BWA_UNKNOWN_ERROR;
  10239. } else if ( money > sd->bank_vault ) {
  10240. return BWA_NO_MONEY;
  10241. } else if ( limit_check > MAX_ZENY ) {
  10242. /* no official response for this scenario exists. */
  10243. clif_colormes(sd->fd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  10244. return BWA_UNKNOWN_ERROR;
  10245. }
  10246. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10247. return BWA_NO_MONEY;
  10248. sd->bank_vault -= money;
  10249. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10250. if( save_settings&CHARSAVE_BANK )
  10251. chrif_save(sd,0);
  10252. return BWA_SUCCESS;
  10253. }
  10254. /**
  10255. * Clear Crimson Marker data from caster
  10256. * @param sd: Player
  10257. **/
  10258. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10259. uint8 i;
  10260. if (!sd)
  10261. return;
  10262. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10263. struct block_list *bl = NULL;
  10264. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10265. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10266. sd->c_marker[i] = 0;
  10267. }
  10268. }
  10269. /**
  10270. * Show version to player
  10271. * @param sd: Player
  10272. **/
  10273. void pc_show_version(struct map_session_data *sd) {
  10274. const char* svn = get_svn_revision();
  10275. char buf[CHAT_SIZE_MAX];
  10276. if( svn[0] != UNKNOWN_VERSION )
  10277. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10278. else {
  10279. const char* git = get_git_hash();
  10280. if( git[0] != UNKNOWN_VERSION )
  10281. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10282. else
  10283. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10284. }
  10285. clif_displaymessage(sd->fd,buf);
  10286. }
  10287. /**
  10288. * Run bonus_script on player
  10289. * @param sd
  10290. * @author [Cydh]
  10291. **/
  10292. void pc_bonus_script(struct map_session_data *sd) {
  10293. int now = gettick();
  10294. struct linkdb_node *node = NULL, *next = NULL;
  10295. if (!sd || !(node = sd->bonus_script.head))
  10296. return;
  10297. while (node) {
  10298. struct s_bonus_script_entry *entry = NULL;
  10299. next = node->next;
  10300. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10301. // Only start timer for new bonus_script
  10302. if (entry->tid == INVALID_TIMER) {
  10303. if (entry->icon != SI_BLANK) // Gives status icon if exist
  10304. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10305. entry->tick += now;
  10306. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10307. }
  10308. if (entry->script)
  10309. run_script(entry->script, 0, sd->bl.id, 0);
  10310. else
  10311. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10312. }
  10313. node = next;
  10314. }
  10315. }
  10316. /**
  10317. * Add bonus_script to player
  10318. * @param sd Player
  10319. * @param script_str Script string
  10320. * @param dur Duration in ms
  10321. * @param icon SI
  10322. * @param flag Flags @see enum e_bonus_script_flags
  10323. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10324. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10325. * @author [Cydh]
  10326. **/
  10327. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum si_type icon, uint16 flag, uint8 type) {
  10328. struct script_code *script = NULL;
  10329. struct linkdb_node *node = NULL;
  10330. struct s_bonus_script_entry *entry = NULL;
  10331. if (!sd)
  10332. return NULL;
  10333. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10334. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10335. return NULL;
  10336. }
  10337. // Duplication checks
  10338. if ((node = sd->bonus_script.head)) {
  10339. while (node) {
  10340. entry = (struct s_bonus_script_entry *)node->data;
  10341. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10342. int newdur = gettick() + dur;
  10343. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10344. settick_timer(entry->tid, newdur);
  10345. script_free_code(script);
  10346. return NULL;
  10347. }
  10348. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10349. break;
  10350. else { // No duplicate bonus
  10351. script_free_code(script);
  10352. return NULL;
  10353. }
  10354. }
  10355. node = node->next;
  10356. }
  10357. }
  10358. CREATE(entry, struct s_bonus_script_entry, 1);
  10359. entry->script_buf = StringBuf_Malloc();
  10360. StringBuf_AppendStr(entry->script_buf, script_str);
  10361. entry->tid = INVALID_TIMER;
  10362. entry->flag = flag;
  10363. entry->icon = icon;
  10364. entry->tick = dur; // Use duration first, on run change to expire time
  10365. entry->type = type;
  10366. entry->script = script;
  10367. sd->bonus_script.count++;
  10368. return entry;
  10369. }
  10370. /**
  10371. * Remove bonus_script data from player
  10372. * @param sd: Target player
  10373. * @param list: Bonus script entry from player
  10374. * @author [Cydh]
  10375. **/
  10376. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10377. if (entry->tid != INVALID_TIMER)
  10378. delete_timer(entry->tid, pc_bonus_script_timer);
  10379. if (entry->script)
  10380. script_free_code(entry->script);
  10381. if (entry->script_buf)
  10382. StringBuf_Free(entry->script_buf);
  10383. if (sd) {
  10384. if (entry->icon != SI_BLANK)
  10385. clif_status_load(&sd->bl, entry->icon, 0);
  10386. if (sd->bonus_script.count > 0)
  10387. sd->bonus_script.count--;
  10388. }
  10389. aFree(entry);
  10390. }
  10391. /**
  10392. * Do final process if no entry left
  10393. * @param sd
  10394. **/
  10395. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10396. if (sd->bonus_script.count == 0) {
  10397. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10398. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10399. linkdb_final(&sd->bonus_script.head);
  10400. }
  10401. }
  10402. /**
  10403. * Timer for bonus_script
  10404. * @param tid
  10405. * @param tick
  10406. * @param id
  10407. * @param data
  10408. * @author [Cydh]
  10409. **/
  10410. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10411. struct map_session_data *sd;
  10412. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10413. sd = map_id2sd(id);
  10414. if (!sd) {
  10415. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10416. return 0;
  10417. }
  10418. if (tid == INVALID_TIMER)
  10419. return 0;
  10420. if (!sd->bonus_script.head || entry == NULL) {
  10421. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10422. return 0;
  10423. }
  10424. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10425. pc_bonus_script_free_entry(sd, entry);
  10426. pc_bonus_script_check_final(sd);
  10427. status_calc_pc(sd,SCO_NONE);
  10428. return 0;
  10429. }
  10430. /**
  10431. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10432. * @param sd: Target player
  10433. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10434. * @author [Cydh]
  10435. **/
  10436. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10437. struct linkdb_node *node = NULL;
  10438. uint16 count = 0;
  10439. if (!sd || !(node = sd->bonus_script.head))
  10440. return;
  10441. while (node) {
  10442. struct linkdb_node *next = node->next;
  10443. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10444. if (entry && (
  10445. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10446. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10447. (flag&entry->flag) || // Matched flag
  10448. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10449. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10450. )
  10451. )
  10452. {
  10453. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10454. pc_bonus_script_free_entry(sd, entry);
  10455. count++;
  10456. }
  10457. node = next;
  10458. }
  10459. pc_bonus_script_check_final(sd);
  10460. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10461. status_calc_pc(sd,SCO_NONE);
  10462. }
  10463. /** [Cydh]
  10464. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10465. * @param sd: Target player
  10466. */
  10467. void pc_cell_basilica(struct map_session_data *sd) {
  10468. nullpo_retv(sd);
  10469. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10470. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10471. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10472. }
  10473. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10474. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10475. }
  10476. /** [Cydh]
  10477. * Get maximum specified parameter for specified class
  10478. * @param class_: sd->class
  10479. * @param sex: sd->status.sex
  10480. * @param flag: parameter will be checked
  10481. * @return max_param
  10482. */
  10483. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10484. int idx = -1, class_ = sd->class_;
  10485. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10486. short max_param = 0;
  10487. switch (param) {
  10488. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10489. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10490. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10491. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10492. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10493. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10494. }
  10495. if (max_param > 0)
  10496. return max_param;
  10497. }
  10498. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10499. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10500. ((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)) :
  10501. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10502. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10503. }
  10504. /**
  10505. * Get max ASPD for player based on Class
  10506. * @param sd Player
  10507. * @return ASPD
  10508. */
  10509. short pc_maxaspd(struct map_session_data *sd) {
  10510. nullpo_ret(sd);
  10511. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10512. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10513. battle_config.max_aspd ));
  10514. }
  10515. /**
  10516. * Calculates total item-group related bonuses for the given item
  10517. * @param sd Player
  10518. * @param nameid Item ID
  10519. * @return Heal rate
  10520. **/
  10521. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10522. short bonus = 0;
  10523. uint8 i;
  10524. if (!sd->itemgrouphealrate_count)
  10525. return bonus;
  10526. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10527. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10528. struct s_item_group_db *group = NULL;
  10529. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10530. continue;
  10531. for (j = 0; j < group->random[0].data_qty; j++) {
  10532. if (group->random[0].data[j].nameid == nameid) {
  10533. bonus += sd->itemgrouphealrate[i]->rate;
  10534. break;
  10535. }
  10536. }
  10537. }
  10538. return bonus;
  10539. }
  10540. /**
  10541. * Calculates total item-group related bonuses for the given item group
  10542. * @param sd Player
  10543. * @param group_id Item Group ID
  10544. * @return Heal rate
  10545. **/
  10546. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10547. short bonus = 0;
  10548. uint8 i;
  10549. if (!sd->itemgrouphealrate_count)
  10550. return bonus;
  10551. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10552. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10553. return sd->itemgrouphealrate[i]->rate;
  10554. }
  10555. return bonus;
  10556. }
  10557. /**
  10558. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10559. * @param eqi Item EQI of enum equip_index
  10560. * @param *equip_index Player's equip_index[]
  10561. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10562. * @return True if item in same inventory index, False if doesn't
  10563. */
  10564. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10565. if (index < 0 || index >= MAX_INVENTORY)
  10566. return true;
  10567. // Dual weapon checks
  10568. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10569. return true;
  10570. // Headgear with Mid & Low location
  10571. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10572. return true;
  10573. // Headgear with Top & Mid or Low location
  10574. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10575. return true;
  10576. // Headgear with Mid & Low location
  10577. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10578. return true;
  10579. // Headgear with Top & Mid or Low location
  10580. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10581. return true;
  10582. return false;
  10583. }
  10584. /**
  10585. * Generate Unique item ID for player
  10586. * @param sd : Player
  10587. * @return A generated Unique item ID
  10588. */
  10589. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10590. nullpo_ret(sd);
  10591. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10592. }
  10593. /**
  10594. * Validating skill from player after logged on
  10595. * @param sd
  10596. **/
  10597. void pc_validate_skill(struct map_session_data *sd) {
  10598. if (sd) {
  10599. uint16 i = 0, count = 0;
  10600. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10601. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10602. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10603. for (i = 0; i < MAX_SKILL; i++) {
  10604. uint16 idx = 0;
  10605. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10606. continue;
  10607. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10608. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10609. count++;
  10610. }
  10611. else
  10612. 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);
  10613. }
  10614. }
  10615. }
  10616. /**
  10617. * Toggle to remember if the questinfo is displayed yet or not.
  10618. * @param qi_display Display flag
  10619. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10620. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10621. **/
  10622. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10623. if (show) {
  10624. // Check if need to be displayed
  10625. if ((*qi_display) != 1) {
  10626. (*qi_display) = 1;
  10627. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10628. }
  10629. }
  10630. else {
  10631. // Check if need to be hide
  10632. if ((*qi_display) != 0) {
  10633. (*qi_display) = 0;
  10634. #if PACKETVER >= 20120410
  10635. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10636. #else
  10637. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10638. #endif
  10639. }
  10640. }
  10641. }
  10642. /**
  10643. * Show available NPC Quest / Event Icon Check [Kisuka]
  10644. * @param sd Player
  10645. **/
  10646. void pc_show_questinfo(struct map_session_data *sd) {
  10647. #if PACKETVER >= 20090218
  10648. struct questinfo *qi = NULL;
  10649. unsigned short i;
  10650. uint8 j;
  10651. int8 mystate = 0;
  10652. bool failed = false;
  10653. nullpo_retv(sd);
  10654. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10655. return;
  10656. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10657. return;
  10658. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10659. qi = &map[sd->bl.m].qi_data[i];
  10660. if (!qi)
  10661. continue;
  10662. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10663. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10664. continue;
  10665. }
  10666. // Level range checks
  10667. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10668. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10669. continue;
  10670. }
  10671. // Quest requirements
  10672. if (qi->req_count) {
  10673. failed = false;
  10674. for (j = 0; j < qi->req_count; j++) {
  10675. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10676. mystate = mystate + (mystate < 1);
  10677. if (mystate != qi->req[j].state) {
  10678. failed = true;
  10679. break;
  10680. }
  10681. }
  10682. if (failed) {
  10683. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10684. continue;
  10685. }
  10686. }
  10687. // Job requirements
  10688. if (qi->jobid_count) {
  10689. failed = true;
  10690. for (j = 0; j < qi->jobid_count; j++) {
  10691. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10692. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10693. failed = false;
  10694. break;
  10695. }
  10696. }
  10697. if (!failed)
  10698. continue;
  10699. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10700. }
  10701. else {
  10702. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10703. }
  10704. }
  10705. #endif
  10706. }
  10707. /**
  10708. * Reinit the questinfo for player when changing map
  10709. * @param sd Player
  10710. **/
  10711. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10712. #if PACKETVER >= 20090218
  10713. nullpo_retv(sd);
  10714. if (sd->qi_display) {
  10715. aFree(sd->qi_display);
  10716. sd->qi_display = NULL;
  10717. }
  10718. sd->qi_count = 0;
  10719. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10720. return;
  10721. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10722. return;
  10723. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10724. #endif
  10725. }
  10726. /**
  10727. * Check if a job is allowed to enter the map
  10728. * @param jobid Job ID see enum e_job or sd->status.class_
  10729. * @param m ID -an index- for direct indexing map[] array
  10730. * @return 1 if job is allowed, 0 otherwise
  10731. **/
  10732. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10733. uint16 idx = 0;
  10734. // Map is other map server.
  10735. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10736. if (m < 0)
  10737. return true;
  10738. if (m >= MAX_MAP_PER_SERVER || !map[m].cell)
  10739. return false;
  10740. if (!pcdb_checkid(jobid))
  10741. return false;
  10742. idx = pc_class2idx(jobid);
  10743. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10744. return true;
  10745. if ((!map_flag_vs2(m) && job_info[idx].noenter_map.zone&1) || // Normal
  10746. (map[m].flag.pvp && job_info[idx].noenter_map.zone&2) || // PVP
  10747. (map_flag_gvg2_no_te(m) && job_info[idx].noenter_map.zone&4) || // GVG
  10748. (map[m].flag.battleground && job_info[idx].noenter_map.zone&8) || // Battleground
  10749. (map_flag_gvg2_te(m) && job_info[idx].noenter_map.zone&16) || // WOE:TE
  10750. (map[m].flag.restricted && job_info[idx].noenter_map.zone&(8*map[m].zone)) // Zone restriction
  10751. )
  10752. return false;
  10753. return true;
  10754. }
  10755. /**
  10756. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  10757. * @param sd
  10758. **/
  10759. void pc_set_costume_view(struct map_session_data *sd) {
  10760. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  10761. struct item_data *id = NULL;
  10762. nullpo_retv(sd);
  10763. head_low = sd->status.head_bottom;
  10764. head_mid = sd->status.head_mid;
  10765. head_top = sd->status.head_top;
  10766. robe = sd->status.robe;
  10767. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  10768. //Added check to prevent sending the same look on multiple slots ->
  10769. //causes client to redraw item on top of itself. (suggested by Lupus)
  10770. // Normal headgear checks
  10771. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10772. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  10773. sd->status.head_bottom = id->look;
  10774. else
  10775. sd->status.head_bottom = 0;
  10776. }
  10777. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10778. if (!(id->equip&(EQP_HEAD_TOP)))
  10779. sd->status.head_mid = id->look;
  10780. else
  10781. sd->status.head_mid = 0;
  10782. }
  10783. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10784. sd->status.head_top = id->look;
  10785. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10786. sd->status.robe = id->look;
  10787. // Costumes check
  10788. if (!map[sd->bl.m].flag.nocostume) {
  10789. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10790. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  10791. sd->status.head_bottom = id->look;
  10792. else
  10793. sd->status.head_bottom = 0;
  10794. }
  10795. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10796. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  10797. sd->status.head_mid = id->look;
  10798. else
  10799. sd->status.head_mid = 0;
  10800. }
  10801. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10802. sd->status.head_top = id->look;
  10803. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10804. sd->status.robe = id->look;
  10805. }
  10806. if (sd->setlook_head_bottom)
  10807. sd->status.head_bottom = sd->setlook_head_bottom;
  10808. if (sd->setlook_head_mid)
  10809. sd->status.head_mid = sd->setlook_head_mid;
  10810. if (sd->setlook_head_top)
  10811. sd->status.head_top = sd->setlook_head_top;
  10812. if (sd->setlook_robe)
  10813. sd->status.robe = sd->setlook_robe;
  10814. if (head_low != sd->status.head_bottom)
  10815. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  10816. if (head_mid != sd->status.head_mid)
  10817. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  10818. if (head_top != sd->status.head_top)
  10819. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  10820. if (robe != sd->status.robe)
  10821. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  10822. }
  10823. /*==========================================
  10824. * pc Init/Terminate
  10825. *------------------------------------------*/
  10826. void do_final_pc(void) {
  10827. db_destroy(itemcd_db);
  10828. do_final_pc_groups();
  10829. ers_destroy(pc_sc_display_ers);
  10830. ers_destroy(pc_itemgrouphealrate_ers);
  10831. ers_destroy(num_reg_ers);
  10832. ers_destroy(str_reg_ers);
  10833. }
  10834. void do_init_pc(void) {
  10835. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10836. pc_readdb();
  10837. pc_read_motd(); // Read MOTD [Valaris]
  10838. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10839. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10840. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10841. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10842. add_timer_func_list(pc_autosave, "pc_autosave");
  10843. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10844. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10845. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10846. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10847. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10848. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10849. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10850. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10851. // 0=day, 1=night [Yor]
  10852. night_flag = battle_config.night_at_start ? 1 : 0;
  10853. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10854. int day_duration = battle_config.day_duration;
  10855. int night_duration = battle_config.night_duration;
  10856. // add night/day timer [Yor]
  10857. add_timer_func_list(map_day_timer, "map_day_timer");
  10858. add_timer_func_list(map_night_timer, "map_night_timer");
  10859. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10860. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10861. }
  10862. do_init_pc_groups();
  10863. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  10864. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10865. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10866. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10867. ers_chunk_size(pc_sc_display_ers, 150);
  10868. ers_chunk_size(num_reg_ers, 300);
  10869. ers_chunk_size(str_reg_ers, 50);
  10870. }