pet.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pet.hpp"
  4. #include <map>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include "../common/db.hpp"
  8. #include "../common/ers.hpp"
  9. #include "../common/malloc.hpp"
  10. #include "../common/nullpo.hpp"
  11. #include "../common/random.hpp"
  12. #include "../common/showmsg.hpp"
  13. #include "../common/strlib.hpp"
  14. #include "../common/timer.hpp"
  15. #include "../common/utilities.hpp"
  16. #include "../common/utils.hpp"
  17. #include "achievement.hpp"
  18. #include "battle.hpp"
  19. #include "chrif.hpp"
  20. #include "clif.hpp"
  21. #include "intif.hpp"
  22. #include "log.hpp"
  23. #include "mob.hpp"
  24. #include "npc.hpp"
  25. #include "pc.hpp"
  26. using namespace rathena;
  27. std::unordered_map<std::string, std::shared_ptr<s_pet_autobonus_wrapper>> pet_autobonuses;
  28. const t_tick MIN_PETTHINKTIME = 100;
  29. const std::string PetDatabase::getDefaultLocation(){
  30. return std::string(db_path) + "/pet_db.yml";
  31. }
  32. uint64 PetDatabase::parseBodyNode( const ryml::NodeRef& node ){
  33. std::string mob_name;
  34. if( !this->asString( node, "Mob", mob_name ) ){
  35. return 0;
  36. }
  37. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( mob_name.c_str() );
  38. if( mob == nullptr ){
  39. this->invalidWarning( node["Mob"], "Mob %s does not exist and cannot be used as a pet.\n", mob_name.c_str() );
  40. return 0;
  41. }
  42. uint16 mob_id = mob->id;
  43. std::shared_ptr<s_pet_db> pet = this->find( mob_id );
  44. bool exists = pet != nullptr;
  45. if( !exists ){
  46. // Check mandatory nodes
  47. if( !this->nodesExist( node, { "EggItem", "Fullness", "CaptureRate" } ) ){
  48. return 0;
  49. }
  50. pet = std::make_shared<s_pet_db>();
  51. pet->class_ = mob_id;
  52. }
  53. if( this->nodeExists( node, "TameItem" ) ){
  54. std::string item_name;
  55. if( !this->asString( node, "TameItem", item_name ) ){
  56. return 0;
  57. }
  58. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  59. if( item == nullptr ){
  60. this->invalidWarning( node["TameItem"], "Taming item %s does not exist.\n", item_name.c_str() );
  61. return 0;
  62. }
  63. pet->itemID = item->nameid;
  64. }else{
  65. if( !exists ){
  66. pet->itemID = 0;
  67. }
  68. }
  69. if( this->nodeExists( node, "EggItem" ) ){
  70. std::string item_name;
  71. if( !this->asString( node, "EggItem", item_name ) ){
  72. return 0;
  73. }
  74. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  75. if( item == nullptr ){
  76. this->invalidWarning( node["EggItem"], "Egg item %s does not exist.\n", item_name.c_str() );
  77. return 0;
  78. }
  79. pet->EggID = item->nameid;
  80. }
  81. if( this->nodeExists( node, "EquipItem" ) ){
  82. std::string item_name;
  83. if( !this->asString( node, "EquipItem", item_name ) ){
  84. return 0;
  85. }
  86. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  87. if( item == nullptr ){
  88. this->invalidWarning( node["EquipItem"], "Equip item %s does not exist.\n", item_name.c_str() );
  89. return 0;
  90. }
  91. pet->AcceID = item->nameid;
  92. }else{
  93. if( !exists ){
  94. pet->AcceID = 0;
  95. }
  96. }
  97. if( this->nodeExists( node, "FoodItem" ) ){
  98. std::string item_name;
  99. if( !this->asString( node, "FoodItem", item_name ) ){
  100. return 0;
  101. }
  102. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  103. if( item == nullptr ){
  104. this->invalidWarning( node["FoodItem"], "Food item %s does not exist.\n", item_name.c_str() );
  105. return 0;
  106. }
  107. pet->FoodID = item->nameid;
  108. }else{
  109. if( !exists ){
  110. pet->FoodID = 0;
  111. }
  112. }
  113. if( this->nodeExists( node, "Fullness" ) ){
  114. uint16 fullness;
  115. if( !this->asUInt16( node, "Fullness", fullness ) ){
  116. return 0;
  117. }
  118. if( fullness > 100 ){
  119. this->invalidWarning( node["Fullness"], "Fullness %hu exceeds maximum of 100. Capping...\n", fullness );
  120. fullness = 100;
  121. }
  122. pet->fullness = fullness;
  123. }
  124. if( this->nodeExists( node, "HungryDelay" ) ){
  125. uint32 delay;
  126. if( !this->asUInt32( node, "HungryDelay", delay ) ){
  127. return 0;
  128. }
  129. pet->hungry_delay = delay * 1000;
  130. }else{
  131. if( !exists ){
  132. pet->hungry_delay = 60000;
  133. }
  134. }
  135. if( this->nodeExists( node, "HungerIncrease" ) ){
  136. int32 increase;
  137. if( !this->asInt32( node, "HungerIncrease", increase ) ){
  138. return 0;
  139. }
  140. pet->hunger_increase = increase;
  141. }else{
  142. if( !exists ){
  143. pet->hunger_increase = 20;
  144. }
  145. }
  146. if( this->nodeExists( node, "IntimacyStart" ) ){
  147. uint32 start;
  148. if( !this->asUInt32( node, "IntimacyStart", start ) ){
  149. return 0;
  150. }
  151. if( start > 10000 ){
  152. this->invalidWarning( node["IntimacyStart"], "IntimacyStart %hu exceeds maximum of 10000. Capping...\n", start );
  153. start = 10000;
  154. }
  155. pet->intimate = start;
  156. }else{
  157. if( !exists ){
  158. pet->intimate = 250;
  159. }
  160. }
  161. if( this->nodeExists( node, "IntimacyFed" ) ){
  162. int32 fed;
  163. if( !this->asInt32( node, "IntimacyFed", fed ) ){
  164. return 0;
  165. }
  166. pet->r_hungry = fed;
  167. }else{
  168. if( !exists ){
  169. pet->r_hungry = 50;
  170. }
  171. }
  172. if( this->nodeExists( node, "IntimacyOverfed" ) ){
  173. int32 overfed;
  174. if( !this->asInt32( node, "IntimacyOverfed", overfed ) ){
  175. return 0;
  176. }
  177. pet->r_full = overfed;
  178. }else{
  179. if( !exists ){
  180. pet->r_full = -100;
  181. }
  182. }
  183. if( this->nodeExists( node, "IntimacyHungry" ) ){
  184. int32 hungry;
  185. if( !this->asInt32( node, "IntimacyHungry", hungry ) ){
  186. return 0;
  187. }
  188. pet->hungry_intimacy_dec = hungry;
  189. }else{
  190. if( !exists ){
  191. pet->hungry_intimacy_dec = -5;
  192. }
  193. }
  194. if( this->nodeExists( node, "IntimacyOwnerDie" ) ){
  195. int32 die;
  196. if( !this->asInt32( node, "IntimacyOwnerDie", die ) ){
  197. return 0;
  198. }
  199. pet->die = die;
  200. }else{
  201. if( !exists ){
  202. pet->die = -20;
  203. }
  204. }
  205. if( this->nodeExists( node, "CaptureRate" ) ){
  206. uint16 rate;
  207. if( !this->asUInt16( node, "CaptureRate", rate ) ){
  208. return 0;
  209. }
  210. if( rate > 10000 ){
  211. this->invalidWarning( node["CaptureRate"], "CaptureRate %hu exceeds maximum of 10000. Capping...\n", rate );
  212. rate = 10000;
  213. }
  214. pet->capture = rate;
  215. }
  216. if( this->nodeExists( node, "SpecialPerformance" ) ){
  217. bool performance;
  218. if( !this->asBool( node, "SpecialPerformance", performance ) ){
  219. return 0;
  220. }
  221. pet->s_perfor = performance;
  222. }else{
  223. if( !exists ){
  224. pet->s_perfor = true;
  225. }
  226. }
  227. if( this->nodeExists( node, "AttackRate" ) ){
  228. uint16 rate;
  229. if( !this->asUInt16( node, "AttackRate", rate ) ){
  230. return 0;
  231. }
  232. if( rate > 10000 ){
  233. this->invalidWarning( node["AttackRate"], "AttackRate %hu exceeds maximum of 10000. Capping...\n", rate );
  234. rate = 10000;
  235. }
  236. pet->attack_rate = rate;
  237. }else{
  238. if( !exists ){
  239. pet->attack_rate = 10001; // unreachable
  240. }
  241. }
  242. if( this->nodeExists( node, "RetaliateRate" ) ){
  243. uint16 rate;
  244. if( !this->asUInt16( node, "RetaliateRate", rate ) ){
  245. return 0;
  246. }
  247. if( rate > 10000 ){
  248. this->invalidWarning( node["RetaliateRate"], "RetaliateRate %hu exceeds maximum of 10000. Capping...\n", rate );
  249. rate = 10000;
  250. }
  251. pet->defence_attack_rate = rate;
  252. }else{
  253. if( !exists ){
  254. pet->defence_attack_rate = 10001; // unreachable
  255. }
  256. }
  257. if( this->nodeExists( node, "ChangeTargetRate" ) ){
  258. uint16 rate;
  259. if( !this->asUInt16( node, "ChangeTargetRate", rate ) ){
  260. return 0;
  261. }
  262. if( rate > 10000 ){
  263. this->invalidWarning( node["ChangeTargetRate"], "ChangeTargetRate %hu exceeds maximum of 10000. Capping...\n", rate );
  264. rate = 10000;
  265. }
  266. pet->change_target_rate = rate;
  267. }else{
  268. if( !exists ){
  269. pet->change_target_rate = 10001; // unreachable
  270. }
  271. }
  272. if( this->nodeExists( node, "AllowAutoFeed" ) ){
  273. bool allow;
  274. if( !this->asBool( node, "AllowAutoFeed", allow ) ){
  275. return 0;
  276. }
  277. pet->allow_autofeed = allow;
  278. }else{
  279. if( !exists ){
  280. pet->allow_autofeed = false;
  281. }
  282. }
  283. if( this->nodeExists( node, "Script" ) ){
  284. std::string script;
  285. if( !this->asString( node, "Script", script ) ){
  286. return 0;
  287. }
  288. if( pet->pet_bonus_script != nullptr ){
  289. script_free_code( pet->pet_bonus_script );
  290. pet->pet_bonus_script = nullptr;
  291. }
  292. pet->pet_bonus_script = parse_script( script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS );
  293. }else{
  294. if( !exists ){
  295. pet->pet_bonus_script = nullptr;
  296. }
  297. }
  298. if( this->nodeExists( node, "SupportScript" ) ){
  299. std::string script;
  300. if( !this->asString( node, "SupportScript", script ) ){
  301. return 0;
  302. }
  303. if( pet->pet_support_script != nullptr ){
  304. script_free_code( pet->pet_support_script );
  305. pet->pet_support_script = nullptr;
  306. }
  307. pet->pet_support_script = parse_script( script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["SupportScript"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS );
  308. }else{
  309. if( !exists ){
  310. pet->pet_support_script = nullptr;
  311. }
  312. }
  313. if( this->nodeExists( node, "Evolution" ) ){
  314. const auto& evolutionsNode = node["Evolution"];
  315. for( const auto& evolutionNode : evolutionsNode ){
  316. std::string target_name;
  317. if( !this->asString( evolutionNode, "Target", target_name ) ){
  318. return 0;
  319. }
  320. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( target_name.c_str() );
  321. if( mob == nullptr ){
  322. this->invalidWarning( evolutionNode["Target"], "Evolution target %s does not exist.\n", target_name.c_str() );
  323. return 0;
  324. }
  325. uint16 targetId = mob->id;
  326. if( !this->nodeExists( evolutionNode, "ItemRequirements" ) ){
  327. this->invalidWarning( evolutionNode, "Missing required node \"ItemRequirements\".\n" );
  328. return 0;
  329. }
  330. std::shared_ptr<s_pet_evo_data> evolution = util::umap_find( pet->evolution_data, targetId );
  331. bool evolution_exists = evolution != nullptr;
  332. if( !evolution_exists ){
  333. evolution = std::make_shared<s_pet_evo_data>();
  334. evolution->target_mob_id = targetId;
  335. }
  336. const auto& requirementsNode = evolutionNode["ItemRequirements"];
  337. for( const auto& requirementNode : requirementsNode ){
  338. std::string item_name;
  339. if( !this->asString( requirementNode, "Item", item_name ) ){
  340. return 0;
  341. }
  342. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  343. if( item == nullptr ){
  344. this->invalidWarning( requirementNode["Item"], "Evolution requirement item %s does not exist.\n", item_name.c_str() );
  345. return 0;
  346. }
  347. uint16 amount;
  348. if( !this->asUInt16( requirementNode, "Amount", amount ) ){
  349. return 0;
  350. }
  351. if( amount > MAX_AMOUNT ){
  352. this->invalidWarning( requirementNode["Amount"], "Amount %u exceeds the maximum of %d.\n", amount, MAX_AMOUNT );
  353. return 0;
  354. }
  355. evolution->requirements[item->nameid] = amount;
  356. }
  357. if( !evolution_exists ){
  358. pet->evolution_data[targetId] = evolution;
  359. }
  360. }
  361. }
  362. if( !exists ){
  363. this->put( mob_id, pet );
  364. }
  365. return 1;
  366. }
  367. /**
  368. * Clear pet support bonuses from memory
  369. * @param sd: Pet owner
  370. */
  371. void pet_clear_support_bonuses(map_session_data *sd) {
  372. nullpo_retv(sd);
  373. if (!sd->pd)
  374. return;
  375. struct pet_data *pd = sd->pd;
  376. if (pd->a_skill) {
  377. aFree(pd->a_skill);
  378. pd->a_skill = NULL;
  379. }
  380. if (pd->s_skill) {
  381. if (pd->s_skill->timer != INVALID_TIMER) {
  382. if (pd->s_skill->id)
  383. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  384. else
  385. delete_timer(pd->s_skill->timer, pet_heal_timer);
  386. }
  387. aFree(pd->s_skill);
  388. pd->s_skill = NULL;
  389. }
  390. if (pd->recovery) {
  391. if (pd->recovery->timer != INVALID_TIMER)
  392. delete_timer(pd->recovery->timer, pet_recovery_timer);
  393. aFree(pd->recovery);
  394. pd->recovery = NULL;
  395. }
  396. if (pd->bonus) {
  397. if (pd->bonus->timer != INVALID_TIMER)
  398. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  399. aFree(pd->bonus);
  400. pd->bonus = NULL;
  401. }
  402. if (pd->loot) {
  403. pet_lootitem_drop(pd, sd);
  404. if (pd->loot->item)
  405. aFree(pd->loot->item);
  406. aFree(pd->loot);
  407. pd->loot = NULL;
  408. }
  409. }
  410. /**
  411. * Apply the proper data on pet owners and pets during pet_db reload.
  412. * @param sd: Pet owner
  413. * @param args: va_list of arguments
  414. * @return 0
  415. */
  416. static int pet_reload_sub( map_session_data *sd, va_list args ){
  417. if( sd->pd == nullptr ){
  418. return 0;
  419. }
  420. struct pet_data *pd = sd->pd;
  421. std::shared_ptr<s_pet_db> pet_db_ptr = pd->get_pet_db();
  422. if( pet_db_ptr == nullptr ){
  423. return 0;
  424. }
  425. // Clear pet bonuses
  426. pet_clear_support_bonuses(sd);
  427. // Relink the pet to the new database entry
  428. pd->db = mob_db.find( pet_db_ptr->class_ );
  429. if( battle_config.pet_status_support ){
  430. run_script( pet_db_ptr->pet_support_script, 0, sd->bl.id, 0 );
  431. }
  432. // Recalculate the player status based on the new data
  433. status_calc_pc( sd, SCO_NONE );
  434. // Recalculate the pet status based on the new data
  435. status_calc_pet( pd, SCO_NONE );
  436. return 0;
  437. }
  438. bool PetDatabase::reload(){
  439. if( !TypesafeYamlDatabase::reload() ){
  440. return false;
  441. }
  442. map_foreachpc( pet_reload_sub );
  443. return true;
  444. }
  445. PetDatabase pet_db;
  446. static struct eri *item_drop_ers; //For loot drops delay structures.
  447. static struct eri *item_drop_list_ers;
  448. /**
  449. * Get the value of the pet's hunger.
  450. * @param pd : pet requesting
  451. * @return Pet's hunger value
  452. */
  453. int pet_hungry_val(struct pet_data *pd)
  454. {
  455. nullpo_ret(pd);
  456. if(pd->pet.hungry > PET_HUNGRY_SATISFIED)
  457. return 4;
  458. else if(pd->pet.hungry > PET_HUNGRY_NEUTRAL)
  459. return 3;
  460. else if(pd->pet.hungry > PET_HUNGRY_HUNGRY)
  461. return 2;
  462. else if(pd->pet.hungry > PET_HUNGRY_VERY_HUNGRY)
  463. return 1;
  464. else
  465. return 0;
  466. }
  467. int16 pet_get_card3_intimacy( int intimacy ){
  468. if( intimacy < PET_INTIMATE_SHY ){
  469. // Awkward
  470. return ( 1 << 1 );
  471. }else if( intimacy < PET_INTIMATE_NEUTRAL ){
  472. // Shy
  473. return ( 2 << 1 );
  474. }else if( intimacy < PET_INTIMATE_CORDIAL ){
  475. // Neutral
  476. return ( 3 << 1 );
  477. }else if( intimacy < PET_INTIMATE_LOYAL ){
  478. // Cordial
  479. return ( 4 << 1 );
  480. }else if( intimacy <= PET_INTIMATE_MAX ){
  481. // Loyal
  482. return ( 5 << 1 );
  483. }else{
  484. // Unknown
  485. return 0;
  486. }
  487. }
  488. /**
  489. * Set the value of the pet's intimacy.
  490. * @param pd : pet requesting
  491. * @param value : new intimacy value
  492. */
  493. void pet_set_intimate(struct pet_data *pd, int value)
  494. {
  495. nullpo_retv(pd);
  496. pd->pet.intimate = min(value, PET_INTIMATE_MAX);
  497. map_session_data *sd = pd->master;
  498. int index = pet_egg_search( sd, pd->pet.pet_id );
  499. if( pd->pet.intimate <= PET_INTIMATE_NONE ){
  500. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER );
  501. }else{
  502. // Remove everything except the rename flag
  503. sd->inventory.u.items_inventory[index].card[3] &= 1;
  504. sd->inventory.u.items_inventory[index].card[3] |= pet_get_card3_intimacy( pd->pet.intimate );
  505. }
  506. if (sd)
  507. status_calc_pc(sd,SCO_NONE);
  508. }
  509. /**
  510. * Create a pet egg.
  511. * @param sd : player requesting
  512. * @param item_id : item ID of tamer
  513. * @return true:success, false:failure
  514. */
  515. bool pet_create_egg(map_session_data *sd, t_itemid item_id)
  516. {
  517. std::shared_ptr<s_pet_db> pet = pet_db_search(item_id, PET_EGG);
  518. if (!pet)
  519. return false; //No pet egg here.
  520. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  521. if( mdb == nullptr ){
  522. return false;
  523. }
  524. if (!pc_inventoryblank(sd))
  525. return false; // Inventory full
  526. sd->catch_target_class = pet->class_;
  527. intif_create_pet(sd->status.account_id, sd->status.char_id, pet->class_, mdb->lv, pet->EggID, 0, pet->intimate, 100, 0, 1, mdb->jname.c_str());
  528. return true;
  529. }
  530. /**
  531. * Make pet drop target.
  532. * @param pd : pet requesting
  533. */
  534. void pet_unlocktarget(struct pet_data *pd)
  535. {
  536. nullpo_retv(pd);
  537. pd->target_id = 0;
  538. pet_stop_attack(pd);
  539. pet_stop_walking(pd,1);
  540. }
  541. /**
  542. * Make pet use attack skill.
  543. * @param pd : pet requesting
  544. * @param target_id : ID of target
  545. * @author [Skotlex]
  546. */
  547. int pet_attackskill(struct pet_data *pd, int target_id)
  548. {
  549. if (!battle_config.pet_status_support || !pd->a_skill ||
  550. (battle_config.pet_equip_required && !pd->pet.equip))
  551. return 0;
  552. if (DIFF_TICK(pd->ud.canact_tick, gettick()) > 0)
  553. return 0;
  554. if (rnd()%100 < (pd->a_skill->rate +pd->pet.intimate*pd->a_skill->bonusrate/1000)) { // Skotlex: Use pet's skill
  555. int inf;
  556. struct block_list *bl;
  557. bl = map_id2bl(target_id);
  558. if(bl == NULL || pd->bl.m != bl->m || bl->prev == NULL || status_isdead(bl) ||
  559. !check_distance_bl(&pd->bl, bl, pd->db->range3))
  560. return 0;
  561. inf = skill_get_inf(pd->a_skill->id);
  562. if (inf & INF_GROUND_SKILL)
  563. unit_skilluse_pos(&pd->bl, bl->x, bl->y, pd->a_skill->id, pd->a_skill->lv);
  564. else //Offensive self skill? Could be stuff like GX.
  565. unit_skilluse_id(&pd->bl,(inf&INF_SELF_SKILL?pd->bl.id:bl->id), pd->a_skill->id, pd->a_skill->lv);
  566. return 1; //Skill invoked.
  567. }
  568. return 0;
  569. }
  570. /**
  571. * Make sure pet can attack from given config values.
  572. * @param pd: pet data
  573. * @param bl: target
  574. * @param type: pet's attack rate type
  575. * @return 0
  576. */
  577. int pet_target_check(struct pet_data *pd,struct block_list *bl,int type)
  578. {
  579. nullpo_ret(pd);
  580. Assert((pd->master == 0) || (pd->master->pd == pd));
  581. if(bl == NULL || bl->type != BL_MOB || bl->prev == NULL ||
  582. pd->pet.intimate < battle_config.pet_support_min_friendly ||
  583. pd->pet.hungry <= PET_HUNGRY_NONE ||
  584. pd->pet.class_ == status_get_class(bl))
  585. return 0;
  586. if(pd->bl.m != bl->m ||
  587. !check_distance_bl(&pd->bl, bl, pd->db->range2))
  588. return 0;
  589. if (!battle_config.pet_master_dead && pc_isdead(pd->master)) {
  590. pet_unlocktarget(pd);
  591. return 0;
  592. }
  593. if (!status_check_skilluse(&pd->bl, bl, 0, 0))
  594. return 0;
  595. std::shared_ptr<s_pet_db> pet_db_ptr = pd->get_pet_db();
  596. int rate;
  597. if(!type) {
  598. rate = pet_db_ptr->attack_rate;
  599. rate = rate * pd->rate_fix / 1000;
  600. if(pet_db_ptr->attack_rate > 0 && rate <= 0)
  601. rate = 1;
  602. } else {
  603. rate = pet_db_ptr->defence_attack_rate;
  604. rate = rate * pd->rate_fix / 1000;
  605. if(pet_db_ptr->defence_attack_rate > 0 && rate <= 0)
  606. rate = 1;
  607. }
  608. if(rnd()%10000 < rate) {
  609. if(pd->target_id == 0 || rnd()%10000 < pet_db_ptr->change_target_rate)
  610. pd->target_id = bl->id;
  611. }
  612. return 0;
  613. }
  614. /**
  615. * Status change check for pets.
  616. * @param sd : player requesting
  617. * @param type : recovery type
  618. * @author [Skotlex]
  619. */
  620. int pet_sc_check(map_session_data *sd, int type)
  621. {
  622. struct pet_data *pd;
  623. nullpo_ret(sd);
  624. pd = sd->pd;
  625. if( pd == NULL
  626. || (battle_config.pet_equip_required && pd->pet.equip == 0)
  627. || pd->recovery == NULL
  628. || pd->recovery->timer != INVALID_TIMER
  629. || pd->recovery->type != type )
  630. return 1;
  631. pd->recovery->timer = add_timer(gettick()+pd->recovery->delay*1000,pet_recovery_timer,sd->bl.id,0);
  632. return 0;
  633. }
  634. /**
  635. * Update pet's hungry value and timer and adjust intimacy based on hunger.
  636. * @param tid : current timer value
  637. * @param tick : how often to update
  638. * @param id : ID of pet owner
  639. * @return 0
  640. */
  641. static TIMER_FUNC(pet_hungry){
  642. map_session_data *sd;
  643. struct pet_data *pd;
  644. int interval;
  645. sd = map_id2sd(id);
  646. if(!sd)
  647. return 1;
  648. if(!sd->status.pet_id || !sd->pd)
  649. return 1;
  650. pd = sd->pd;
  651. if(pd->pet_hungry_timer != tid) {
  652. ShowError("pet_hungry_timer %d != %d\n",pd->pet_hungry_timer,tid);
  653. return 0;
  654. }
  655. pd->pet_hungry_timer = INVALID_TIMER;
  656. if (pd->pet.intimate <= PET_INTIMATE_NONE)
  657. return 1; //You lost the pet already, the rest is irrelevant.
  658. std::shared_ptr<s_pet_db> pet_db_ptr = pd->get_pet_db();
  659. if (battle_config.pet_hungry_delay_rate != 100)
  660. interval = (pet_db_ptr->hungry_delay*battle_config.pet_hungry_delay_rate) / 100;
  661. else
  662. interval = pet_db_ptr->hungry_delay;
  663. pd->pet.hungry -= pet_db_ptr->fullness;
  664. if( pd->pet.hungry < PET_HUNGRY_NONE ) {
  665. pet_stop_attack(pd);
  666. pd->pet.hungry = PET_HUNGRY_NONE;
  667. pet_set_intimate(pd, pd->pet.intimate + pet_db_ptr->hungry_intimacy_dec);
  668. if( pd->pet.intimate <= PET_INTIMATE_NONE ) {
  669. pet_set_intimate(pd, PET_INTIMATE_NONE);
  670. pd->status.speed = pd->get_pet_walk_speed();
  671. }
  672. status_calc_pet(pd,SCO_NONE);
  673. clif_send_petdata(sd,pd,1,pd->pet.intimate);
  674. interval = 20000; // While starving, it's every 20 seconds
  675. }
  676. clif_send_petdata(sd,pd,2,pd->pet.hungry);
  677. if( battle_config.feature_pet_autofeed && pd->pet.autofeed && pd->pet.hungry <= battle_config.feature_pet_autofeed_rate ){
  678. pet_food( sd, pd );
  679. }
  680. interval = max(interval, 1);
  681. pd->pet_hungry_timer = add_timer(tick+interval,pet_hungry,sd->bl.id,0);
  682. return 0;
  683. }
  684. /**
  685. * Search pet database for given value and type.
  686. * @param key : value to search for
  687. * @param type : pet type to search for (Catch, Egg, Equip, Food)
  688. * @return Pet DB pointer on success, NULL on failure
  689. */
  690. std::shared_ptr<s_pet_db> pet_db_search( int key, enum e_pet_itemtype type ){
  691. for( auto &pair : pet_db ){
  692. std::shared_ptr<s_pet_db> pet = pair.second;
  693. switch(type) {
  694. case PET_CATCH: if(pet->itemID == key) return pet; break;
  695. case PET_EGG: if(pet->EggID == key) return pet; break;
  696. case PET_EQUIP: if(pet->AcceID == key) return pet; break;
  697. case PET_FOOD: if(pet->FoodID == key) return pet; break;
  698. default:
  699. ShowError( "pet_db_search: Unsupported type %d\n", type );
  700. return nullptr;
  701. }
  702. }
  703. return nullptr;
  704. }
  705. /**
  706. * Remove hunger timer from pet.
  707. * @param pd : pet requesting
  708. * @return 1
  709. */
  710. int pet_hungry_timer_delete(struct pet_data *pd)
  711. {
  712. nullpo_ret(pd);
  713. if(pd->pet_hungry_timer != INVALID_TIMER) {
  714. delete_timer(pd->pet_hungry_timer,pet_hungry);
  715. pd->pet_hungry_timer = INVALID_TIMER;
  716. }
  717. return 1;
  718. }
  719. /**
  720. * Make a pet perform/dance.
  721. * @param sd : player requesting
  722. * @param pd : pet requesting
  723. * @return 1
  724. */
  725. static int pet_performance(map_session_data *sd, struct pet_data *pd)
  726. {
  727. int val;
  728. if (pd->pet.intimate > PET_INTIMATE_LOYAL)
  729. val = pd->get_pet_db()->s_perfor ? 4 : 3;
  730. else if(pd->pet.intimate > PET_INTIMATE_CORDIAL) //TODO: this is way too high
  731. val = 2;
  732. else
  733. val = 1;
  734. pet_stop_walking(pd,2000<<8);
  735. clif_pet_performance(pd, rnd()%val + 1);
  736. pet_lootitem_drop(pd,NULL);
  737. return 1;
  738. }
  739. /**
  740. * Return a pet to it's egg.
  741. * @param sd : player requesting
  742. * @param pd : pet requesting
  743. * @return true if everything went well, false if the egg is not found in the inventory.
  744. */
  745. bool pet_return_egg( map_session_data *sd, struct pet_data *pd ){
  746. pet_lootitem_drop(pd,sd);
  747. int i = pet_egg_search( sd, pd->pet.pet_id );
  748. if( i == -1 ){
  749. return false;
  750. }
  751. sd->inventory.u.items_inventory[i].attribute = 0;
  752. sd->inventory.dirty = true;
  753. pd->pet.incubate = 1;
  754. #if PACKETVER >= 20180704
  755. clif_inventorylist(sd);
  756. clif_send_petdata(sd, pd, 6, 0);
  757. #endif
  758. unit_free(&pd->bl,CLR_OUTSIGHT);
  759. status_calc_pc(sd,SCO_NONE);
  760. sd->status.pet_id = 0;
  761. return true;
  762. }
  763. /**
  764. * Load initial pet data when hatching/creating.
  765. * @param sd : player requesting
  766. * @param pet : pet requesting
  767. * @return True on success or false otherwise
  768. */
  769. bool pet_data_init(map_session_data *sd, struct s_pet *pet)
  770. {
  771. struct pet_data *pd;
  772. int interval = 0;
  773. nullpo_retr(false, sd);
  774. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  775. if(sd->status.account_id != pet->account_id || sd->status.char_id != pet->char_id) {
  776. sd->status.pet_id = 0;
  777. return false;
  778. }
  779. if (sd->status.pet_id != pet->pet_id) {
  780. if (sd->status.pet_id) {
  781. //Wrong pet?? Set incubate to no and send it back for saving.
  782. pet->incubate = 1;
  783. intif_save_petdata(sd->status.account_id,pet);
  784. sd->status.pet_id = 0;
  785. return false;
  786. }
  787. //The pet_id value was lost? odd... restore it.
  788. sd->status.pet_id = pet->pet_id;
  789. }
  790. std::shared_ptr<s_pet_db> pet_db_ptr = pet_db.find(pet->class_);
  791. if( pet_db_ptr == nullptr ){
  792. sd->status.pet_id = 0;
  793. return false;
  794. }
  795. sd->pd = pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
  796. pd->bl.type = BL_PET;
  797. pd->bl.id = npc_get_new_npc_id();
  798. pd->master = sd;
  799. pd->db = mob_db.find(pet->class_);
  800. memcpy(&pd->pet, pet, sizeof(struct s_pet));
  801. status_set_viewdata(&pd->bl, pet->class_);
  802. unit_dataset(&pd->bl);
  803. pd->ud.dir = sd->ud.dir;
  804. pd->bl.m = sd->bl.m;
  805. pd->bl.x = sd->bl.x;
  806. pd->bl.y = sd->bl.y;
  807. unit_calc_pos(&pd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  808. pd->bl.x = pd->ud.to_x;
  809. pd->bl.y = pd->ud.to_y;
  810. map_addiddb(&pd->bl);
  811. status_calc_pet(pd,SCO_FIRST);
  812. pd->last_thinktime = gettick();
  813. pd->state.skillbonus = 0;
  814. if( pet_db_ptr != nullptr ) {
  815. if( battle_config.pet_status_support )
  816. run_script(pet_db_ptr->pet_support_script,0,sd->bl.id,0);
  817. if( pet_db_ptr->pet_bonus_script )
  818. status_calc_pc(sd,SCO_NONE);
  819. if( battle_config.pet_hungry_delay_rate != 100 )
  820. interval = pet_db_ptr->hungry_delay * battle_config.pet_hungry_delay_rate / 100;
  821. else
  822. interval = pet_db_ptr->hungry_delay;
  823. if (pd->pet.hungry <= PET_HUNGRY_NONE)
  824. interval = 20000; // While starving, it's every 20 seconds
  825. }
  826. interval = max(interval, 1);
  827. pd->pet_hungry_timer = add_timer(gettick() + interval, pet_hungry, sd->bl.id, 0);
  828. pd->masterteleport_timer = INVALID_TIMER;
  829. if( !pet->rename_flag ){
  830. safestrncpy( sd->pd->pet.name, pd->db->jname.c_str(), NAME_LENGTH );
  831. }
  832. return true;
  833. }
  834. /**
  835. * Begin hatching a pet.
  836. * @param sd : player requesting
  837. * @param pet : pet requesting
  838. */
  839. int pet_birth_process(map_session_data *sd, struct s_pet *pet)
  840. {
  841. nullpo_retr(1, sd);
  842. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  843. if(sd->status.pet_id && pet->incubate == 1) {
  844. sd->status.pet_id = 0;
  845. return 1;
  846. }
  847. pet->incubate = 0;
  848. pet->account_id = sd->status.account_id;
  849. pet->char_id = sd->status.char_id;
  850. sd->status.pet_id = pet->pet_id;
  851. if(!pet_data_init(sd, pet)) {
  852. return 1;
  853. }
  854. intif_save_petdata(sd->status.account_id,pet);
  855. if (save_settings&CHARSAVE_PET)
  856. chrif_save(sd, CSAVE_INVENTORY); //is it REALLY Needed to save the char for hatching a pet? [Skotlex]
  857. if(sd->bl.prev != NULL) {
  858. if(map_addblock(&sd->pd->bl))
  859. return 1;
  860. clif_spawn(&sd->pd->bl);
  861. clif_send_petdata(sd,sd->pd, 0,0);
  862. clif_send_petdata(sd,sd->pd, 5,battle_config.pet_hair_style);
  863. #if PACKETVER >= 20180704
  864. clif_send_petdata(sd, sd->pd, 6, 1);
  865. #endif
  866. clif_pet_equip_area(sd->pd);
  867. clif_send_petstatus(sd);
  868. clif_pet_autofeed_status(sd,true);
  869. }
  870. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  871. return 0;
  872. }
  873. /**
  874. * Finalize hatching process and load pet to client.
  875. * @param account_id : account ID of owner
  876. * @param p : pet requesting
  877. * @param flag : 1:stop loading of pet
  878. * @return 0:success, 1:failure
  879. */
  880. int pet_recv_petdata(uint32 account_id,struct s_pet *p,int flag)
  881. {
  882. map_session_data *sd;
  883. sd = map_id2sd(account_id);
  884. if(sd == NULL)
  885. return 1;
  886. if(flag == 1) {
  887. sd->status.pet_id = 0;
  888. return 1;
  889. }
  890. if(p->incubate == 1) {
  891. int i = pet_egg_search(sd, p->pet_id);
  892. if(i == -1) {
  893. ShowError("pet_recv_petdata: Hatching pet (%d:%s) aborted, couldn't find egg in inventory!\n",p->pet_id, p->name);
  894. sd->status.pet_id = 0;
  895. return 1;
  896. }
  897. // Hide egg from inventory.
  898. // Set pet egg to broken, before the inventory gets saved
  899. sd->inventory.u.items_inventory[i].attribute = 1;
  900. // Hatch the pet
  901. pet_birth_process( sd, p );
  902. } else {
  903. pet_data_init(sd,p);
  904. if(sd->pd && sd->bl.prev != NULL) {
  905. if(map_addblock(&sd->pd->bl))
  906. return 1;
  907. clif_spawn(&sd->pd->bl);
  908. clif_send_petdata(sd,sd->pd,0,0);
  909. clif_send_petdata(sd,sd->pd,5,battle_config.pet_hair_style);
  910. clif_pet_equip_area(sd->pd);
  911. clif_send_petstatus(sd);
  912. }
  913. }
  914. return 0;
  915. }
  916. /**
  917. * Selected pet egg to hatch.
  918. * @param sd : player requesting
  919. * @param egg_index : egg index value in inventory
  920. * @return 0
  921. */
  922. int pet_select_egg(map_session_data *sd,short egg_index)
  923. {
  924. nullpo_ret(sd);
  925. if(egg_index < 0 || egg_index >= MAX_INVENTORY)
  926. return 0; //Forged packet!
  927. if(sd->trade_partner) //The player have trade in progress.
  928. return 0;
  929. std::shared_ptr<s_pet_db> pet = pet_db_search(sd->inventory.u.items_inventory[egg_index].nameid, PET_EGG);
  930. if (!pet) {
  931. ShowError("pet does not exist, egg id %u\n", sd->inventory.u.items_inventory[egg_index].nameid);
  932. return 0;
  933. }
  934. if(sd->inventory.u.items_inventory[egg_index].card[0] == CARD0_PET)
  935. intif_request_petdata(sd->status.account_id, sd->status.char_id, MakeDWord(sd->inventory.u.items_inventory[egg_index].card[1], sd->inventory.u.items_inventory[egg_index].card[2]) );
  936. else
  937. ShowError("wrong egg item inventory %d\n",egg_index);
  938. return 0;
  939. }
  940. /**
  941. * Display the success/failure roulette wheel when trying to catch monster.
  942. * @param sd : player requesting
  943. * @param target_class : monster ID of pet to catch
  944. * @return 0
  945. */
  946. int pet_catch_process1(map_session_data *sd,int target_class)
  947. {
  948. nullpo_ret(sd);
  949. if (map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  950. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  951. return 0;
  952. }
  953. sd->catch_target_class = target_class;
  954. clif_catch_process(sd);
  955. return 0;
  956. }
  957. /**
  958. * Begin the actual catching process of a monster.
  959. * @param sd : player requesting
  960. * @param target_id : monster ID of pet to catch
  961. * @return 0:success, 1:failure
  962. */
  963. int pet_catch_process2(map_session_data* sd, int target_id)
  964. {
  965. struct mob_data* md;
  966. int pet_catch_rate = 0;
  967. nullpo_retr(1, sd);
  968. md = (struct mob_data*)map_id2bl(target_id);
  969. if(!md || md->bl.type != BL_MOB || md->bl.prev == NULL) { // Invalid inputs/state, abort capture.
  970. clif_pet_roulette(sd,0);
  971. sd->catch_target_class = PET_CATCH_FAIL;
  972. sd->itemid = 0;
  973. sd->itemindex = -1;
  974. return 1;
  975. }
  976. if (map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  977. clif_pet_roulette(sd, 0);
  978. sd->catch_target_class = PET_CATCH_FAIL;
  979. sd->itemid = 0;
  980. sd->itemindex = -1;
  981. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  982. return 1;
  983. }
  984. //FIXME: delete taming item here, if this was an item-invoked capture and the item was flagged as delay-consume [ultramage]
  985. std::shared_ptr<s_pet_db> pet = pet_db.find(md->mob_id);
  986. // If the target is a valid pet, we have a few exceptions
  987. if( pet ){
  988. //catch_target_class == PET_CATCH_UNIVERSAL is used for universal lures (except bosses for now). [Skotlex]
  989. if (sd->catch_target_class == PET_CATCH_UNIVERSAL && !status_has_mode(&md->status,MD_STATUSIMMUNE)){
  990. sd->catch_target_class = md->mob_id;
  991. //catch_target_class == PET_CATCH_UNIVERSAL_ITEM is used for catching any monster required the lure item used
  992. }else if (sd->catch_target_class == PET_CATCH_UNIVERSAL_ITEM && sd->itemid == pet->itemID){
  993. sd->catch_target_class = md->mob_id;
  994. }
  995. }
  996. if(sd->catch_target_class != md->mob_id || !pet) {
  997. clif_emotion(&md->bl, ET_ANGER); //mob will do /ag if wrong lure is used on them.
  998. clif_pet_roulette(sd,0);
  999. sd->catch_target_class = PET_CATCH_FAIL;
  1000. return 1;
  1001. }
  1002. if( battle_config.pet_distance_check && distance_bl( &sd->bl, &md->bl ) > battle_config.pet_distance_check ){
  1003. clif_pet_roulette( sd, 0 );
  1004. sd->catch_target_class = PET_CATCH_FAIL;
  1005. return 1;
  1006. }
  1007. status_change* tsc = status_get_sc( &md->bl );
  1008. if( battle_config.pet_hide_check && tsc && ( tsc->getSCE(SC_HIDING) || tsc->getSCE(SC_CLOAKING) || tsc->getSCE(SC_CAMOUFLAGE) || tsc->getSCE(SC_NEWMOON) || tsc->getSCE(SC_CLOAKINGEXCEED) ) ){
  1009. clif_pet_roulette( sd, 0 );
  1010. sd->catch_target_class = PET_CATCH_FAIL;
  1011. return 1;
  1012. }
  1013. if( battle_config.pet_legacy_formula ){
  1014. pet_catch_rate = ( pet->capture + ( sd->status.base_level - md->level ) * 30 + sd->battle_status.luk * 20 ) * ( 200 - get_percentage( md->status.hp, md->status.max_hp ) ) / 100;
  1015. }else{
  1016. pet_catch_rate = pet->capture + ( ( 100 - get_percentage( md->status.hp, md->status.max_hp ) ) * pet->capture ) / 100;
  1017. }
  1018. if(pet_catch_rate < 1)
  1019. pet_catch_rate = 1;
  1020. if(battle_config.pet_catch_rate != 100)
  1021. pet_catch_rate = (pet_catch_rate*battle_config.pet_catch_rate)/100;
  1022. if(rnd()%10000 < pet_catch_rate) {
  1023. achievement_update_objective(sd, AG_TAMING, 1, md->mob_id);
  1024. unit_remove_map(&md->bl,CLR_OUTSIGHT);
  1025. status_kill(&md->bl);
  1026. clif_pet_roulette(sd,1);
  1027. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  1028. intif_create_pet(sd->status.account_id, sd->status.char_id, pet->class_, mdb->lv, pet->EggID, 0, pet->intimate, 100, 0, 1, mdb->jname.c_str());
  1029. } else {
  1030. clif_pet_roulette(sd,0);
  1031. sd->catch_target_class = PET_CATCH_FAIL;
  1032. }
  1033. return 0;
  1034. }
  1035. /**
  1036. * Is invoked _only_ when a new pet has been created is a product of packet 0x3880
  1037. * see mapif_pet_created@int_pet.cpp for more information.
  1038. * Handles new pet data from inter-server and prepares item information to add pet egg.
  1039. * @param account_id : account ID of owner
  1040. * @param pet_class : class of pet
  1041. * @param pet_id : pet ID otherwise means failure
  1042. * @return true : success, false : failure
  1043. **/
  1044. bool pet_get_egg(uint32 account_id, short pet_class, int pet_id ) {
  1045. map_session_data *sd;
  1046. struct item tmp_item;
  1047. int ret = 0;
  1048. if( pet_id == 0 || pet_class == 0 )
  1049. return false;
  1050. sd = map_id2sd(account_id);
  1051. if( sd == NULL )
  1052. return false;
  1053. // i = pet_search_petDB_index(sd->catch_target_class,PET_CLASS);
  1054. // issue: 8150
  1055. // Before this change in cases where more than one pet egg were requested in a short
  1056. // period of time it wasn't possible to know which kind of egg was being requested after
  1057. // the first request. [Panikon]
  1058. std::shared_ptr<s_pet_db> pet = pet_db.find(pet_class);
  1059. sd->catch_target_class = PET_CATCH_FAIL;
  1060. if(!pet) {
  1061. intif_delete_petdata(pet_id);
  1062. return false;
  1063. }
  1064. memset(&tmp_item,0,sizeof(tmp_item));
  1065. tmp_item.nameid = pet->EggID;
  1066. tmp_item.identify = 1;
  1067. tmp_item.card[0] = CARD0_PET;
  1068. tmp_item.card[1] = GetWord(pet_id,0);
  1069. tmp_item.card[2] = GetWord(pet_id,1);
  1070. tmp_item.card[3] = 0; //New pets are not named.
  1071. tmp_item.card[3] |= pet_get_card3_intimacy( pet->intimate ); // Store intimacy status based on initial intimacy
  1072. if((ret = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER))) {
  1073. clif_additem(sd,0,0,ret);
  1074. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  1075. }
  1076. return true;
  1077. }
  1078. static int pet_unequipitem(map_session_data *sd, struct pet_data *pd);
  1079. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap);
  1080. /**
  1081. * Pet menu options.
  1082. * @param sd : player requesting
  1083. * @param menunum : menu option chosen
  1084. * @return 0:success, 1:failure
  1085. */
  1086. int pet_menu(map_session_data *sd,int menunum)
  1087. {
  1088. nullpo_ret(sd);
  1089. if (sd->pd == NULL)
  1090. return 1;
  1091. //You lost the pet already.
  1092. if(!sd->status.pet_id || sd->pd->pet.intimate <= PET_INTIMATE_NONE || sd->pd->pet.incubate)
  1093. return 1;
  1094. switch(menunum) {
  1095. case 0:
  1096. clif_send_petstatus(sd);
  1097. break;
  1098. case 1:
  1099. pet_food(sd, sd->pd);
  1100. break;
  1101. case 2:
  1102. pet_performance(sd, sd->pd);
  1103. break;
  1104. case 3:
  1105. pet_return_egg(sd, sd->pd);
  1106. break;
  1107. case 4:
  1108. pet_unequipitem(sd, sd->pd);
  1109. break;
  1110. }
  1111. return 0;
  1112. }
  1113. /**
  1114. * Apply pet's new name.
  1115. * @param sd : player requesting
  1116. * @param name : new pet name
  1117. * @return 0:success, 1:failure
  1118. */
  1119. int pet_change_name(map_session_data *sd,char *name)
  1120. {
  1121. int i;
  1122. struct pet_data *pd;
  1123. nullpo_retr(1, sd);
  1124. pd = sd->pd;
  1125. if((pd == NULL) || (pd->pet.rename_flag == 1 && !battle_config.pet_rename))
  1126. return 1;
  1127. for(i = 0; i < NAME_LENGTH && name[i]; i++) {
  1128. if( !(name[i]&0xe0) || name[i]==0x7f)
  1129. return 1;
  1130. }
  1131. return intif_rename_pet(sd, name);
  1132. }
  1133. /**
  1134. * Request to change pet's name.
  1135. * @param sd : player requesting
  1136. * @param name : new pet name
  1137. * @param flag : 1:cannot use this name
  1138. * @return 1:success, 0:failure
  1139. */
  1140. int pet_change_name_ack(map_session_data *sd, char* name, int flag)
  1141. {
  1142. struct pet_data *pd = sd->pd;
  1143. if (!pd)
  1144. return 0;
  1145. normalize_name(name," ");//bugreport:3032
  1146. if ( !flag || !strlen(name) ) {
  1147. clif_displaymessage(sd->fd, msg_txt(sd,280)); // You cannot use this name for your pet.
  1148. clif_send_petstatus(sd); //Send status so client knows pet name change got rejected.
  1149. return 0;
  1150. }
  1151. memcpy(pd->pet.name, name, NAME_LENGTH);
  1152. clif_name_area(&pd->bl);
  1153. pd->pet.rename_flag = 1;
  1154. clif_pet_equip_area(pd);
  1155. clif_send_petstatus(sd);
  1156. int index = pet_egg_search( sd, pd->pet.pet_id );
  1157. if( index >= 0 ){
  1158. sd->inventory.u.items_inventory[index].card[3] |= 1;
  1159. }
  1160. return 1;
  1161. }
  1162. /**
  1163. * Apply a pet's equipment.
  1164. * @param sd : player requesting
  1165. * @param index : index value of item
  1166. * @return 0:success, 1:failure
  1167. */
  1168. int pet_equipitem(map_session_data *sd,int index)
  1169. {
  1170. struct pet_data *pd;
  1171. nullpo_retr(1, sd);
  1172. pd = sd->pd;
  1173. if (!pd)
  1174. return 1;
  1175. std::shared_ptr<s_pet_db> pet_db_ptr = pd->get_pet_db();
  1176. if( pet_db_ptr == nullptr ){
  1177. return 1;
  1178. }
  1179. t_itemid nameid = sd->inventory.u.items_inventory[index].nameid;
  1180. if(pet_db_ptr->AcceID == 0 || nameid != pet_db_ptr->AcceID || pd->pet.equip != 0) {
  1181. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index );
  1182. return 1;
  1183. }
  1184. pc_delitem(sd,index,1,0,0,LOG_TYPE_OTHER);
  1185. pd->pet.equip = nameid;
  1186. status_set_viewdata(&pd->bl, pd->pet.class_); //Updates view_data.
  1187. clif_pet_equip_area(pd);
  1188. if (battle_config.pet_equip_required) { // Skotlex: start support timers if need
  1189. t_tick tick = gettick();
  1190. if (pd->s_skill && pd->s_skill->timer == INVALID_TIMER) {
  1191. if (pd->s_skill->id)
  1192. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_skill_support_timer, sd->bl.id, 0);
  1193. else
  1194. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_heal_timer, sd->bl.id, 0);
  1195. }
  1196. if (pd->bonus && pd->bonus->timer == INVALID_TIMER)
  1197. pd->bonus->timer=add_timer(tick+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  1198. }
  1199. return 0;
  1200. }
  1201. /**
  1202. * Remove a pet's equipment.
  1203. * @param sd : player requesting
  1204. * @param pd : pet requesting
  1205. * @return 0:success, 1:failure
  1206. */
  1207. static int pet_unequipitem(map_session_data *sd, struct pet_data *pd)
  1208. {
  1209. struct item tmp_item;
  1210. unsigned char flag = 0;
  1211. if(pd->pet.equip == 0)
  1212. return 1;
  1213. t_itemid nameid = pd->pet.equip;
  1214. pd->pet.equip = 0;
  1215. status_set_viewdata(&pd->bl, pd->pet.class_);
  1216. clif_pet_equip_area(pd);
  1217. memset(&tmp_item,0,sizeof(tmp_item));
  1218. tmp_item.nameid = nameid;
  1219. tmp_item.identify = 1;
  1220. if((flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_OTHER))) {
  1221. clif_additem(sd,0,0,flag);
  1222. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  1223. }
  1224. if( battle_config.pet_equip_required ) { // Skotlex: halt support timers if needed
  1225. if( pd->state.skillbonus ) {
  1226. pd->state.skillbonus = 0;
  1227. status_calc_pc(sd,SCO_NONE);
  1228. }
  1229. if( pd->s_skill && pd->s_skill->timer != INVALID_TIMER ) {
  1230. if( pd->s_skill->id )
  1231. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  1232. else
  1233. delete_timer(pd->s_skill->timer, pet_heal_timer);
  1234. pd->s_skill->timer = INVALID_TIMER;
  1235. }
  1236. if( pd->bonus && pd->bonus->timer != INVALID_TIMER ) {
  1237. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  1238. pd->bonus->timer = INVALID_TIMER;
  1239. }
  1240. }
  1241. return 0;
  1242. }
  1243. /**
  1244. * Feed a pet and update intimacy.
  1245. * @param sd : playerr requesting
  1246. * @param pd : pet requesting
  1247. * @return 0:success, 1:failure
  1248. */
  1249. int pet_food(map_session_data *sd, struct pet_data *pd)
  1250. {
  1251. nullpo_retr(1, sd);
  1252. nullpo_retr(1, pd);
  1253. std::shared_ptr<s_pet_db> pet_db_ptr = pd->get_pet_db();
  1254. int i,k;
  1255. k = pet_db_ptr->FoodID;
  1256. i = pc_search_inventory(sd,k);
  1257. if( i < 0 ) {
  1258. clif_pet_food(sd,k,0);
  1259. return 1;
  1260. }
  1261. pc_delitem(sd,i,1,0,0,LOG_TYPE_CONSUME);
  1262. if (pd->pet.hungry > PET_HUNGRY_SATISFIED) {
  1263. pet_set_intimate(pd, pd->pet.intimate + pet_db_ptr->r_full);
  1264. if (pd->pet.intimate <= PET_INTIMATE_NONE) {
  1265. pet_set_intimate(pd, PET_INTIMATE_NONE);
  1266. pet_stop_attack(pd);
  1267. pd->status.speed = pd->get_pet_walk_speed();
  1268. }
  1269. }
  1270. else {
  1271. if( battle_config.pet_friendly_rate != 100 )
  1272. k = (pet_db_ptr->r_hungry * battle_config.pet_friendly_rate) / 100;
  1273. else
  1274. k = pet_db_ptr->r_hungry;
  1275. if( pd->pet.hungry > PET_HUNGRY_NEUTRAL) {
  1276. k >>= 1;
  1277. k = max(k, 1);
  1278. }
  1279. pet_set_intimate(pd, pd->pet.intimate + k);
  1280. }
  1281. status_calc_pet(pd,SCO_NONE);
  1282. pd->pet.hungry = min(pd->pet.hungry + pet_db_ptr->hunger_increase, PET_HUNGRY_STUFFED);
  1283. if( pd->pet.hungry > 100 )
  1284. pd->pet.hungry = 100;
  1285. log_feeding(sd, LOG_FEED_PET, pet_db_ptr->FoodID);
  1286. clif_send_petdata(sd,pd,2,pd->pet.hungry);
  1287. clif_send_petdata(sd,pd,1,pd->pet.intimate);
  1288. clif_pet_food(sd, pet_db_ptr->FoodID,1);
  1289. return 0;
  1290. }
  1291. /**
  1292. * Let a pet walk around and update walk timer.
  1293. * @param pd : pet requesting
  1294. * @param tick : last walk time
  1295. * @return 1:success, 0:failure
  1296. */
  1297. static int pet_randomwalk(struct pet_data *pd,t_tick tick)
  1298. {
  1299. nullpo_ret(pd);
  1300. Assert((pd->master == 0) || (pd->master->pd == pd));
  1301. if(DIFF_TICK(pd->next_walktime,tick) < 0 && unit_can_move(&pd->bl)) {
  1302. const int retrycount = 20;
  1303. int i, c, d = 12-pd->move_fail_count;
  1304. if(d < 5)
  1305. d = 5;
  1306. for(i = 0; i < retrycount; i++) {
  1307. int r = rnd(), x, y;
  1308. x = pd->bl.x+r%(d*2+1)-d;
  1309. y = pd->bl.y+r/(d*2+1)%(d*2+1)-d;
  1310. if(map_getcell(pd->bl.m,x,y,CELL_CHKPASS) && unit_walktoxy(&pd->bl,x,y,0)) {
  1311. pd->move_fail_count = 0;
  1312. break;
  1313. }
  1314. if(i + 1 >= retrycount) {
  1315. pd->move_fail_count++;
  1316. if(pd->move_fail_count > 1000) {
  1317. ShowWarning("Pet can't move. Holding position %d, class = %d\n",pd->bl.id,pd->pet.class_);
  1318. pd->move_fail_count = 0;
  1319. pd->ud.canmove_tick = tick + 60000;
  1320. return 0;
  1321. }
  1322. }
  1323. }
  1324. for(i = c = 0; i < pd->ud.walkpath.path_len; i++) {
  1325. if( direction_diagonal( pd->ud.walkpath.path[i] ) )
  1326. c += pd->status.speed*MOVE_DIAGONAL_COST/MOVE_COST;
  1327. else
  1328. c += pd->status.speed;
  1329. }
  1330. pd->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME+c;
  1331. return 1;
  1332. }
  1333. return 0;
  1334. }
  1335. /**
  1336. * Pet AI decision making when supporting master.
  1337. * @param pd : pet requesting
  1338. * @param sd : player requesting
  1339. * @param tick : last support time
  1340. * @return 0
  1341. */
  1342. static int pet_ai_sub_hard(struct pet_data *pd, map_session_data *sd, t_tick tick)
  1343. {
  1344. struct block_list *target = NULL;
  1345. if(pd->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL)
  1346. return 0;
  1347. if(DIFF_TICK(tick,pd->last_thinktime) < MIN_PETTHINKTIME)
  1348. return 0;
  1349. pd->last_thinktime = tick;
  1350. if(pd->ud.attacktimer != INVALID_TIMER || pd->ud.skilltimer != INVALID_TIMER || pd->bl.m != sd->bl.m)
  1351. return 0;
  1352. if(pd->ud.walktimer != INVALID_TIMER && pd->ud.walkpath.path_pos <= 2)
  1353. return 0; // No thinking when you just started to walk.
  1354. if(pd->pet.intimate <= PET_INTIMATE_NONE) {
  1355. // Pet should just... well, random walk.
  1356. pet_randomwalk(pd,tick);
  1357. return 0;
  1358. }
  1359. if (!check_distance_bl(&sd->bl, &pd->bl, pd->db->range3)) {
  1360. // Master too far, chase.
  1361. if(pd->target_id)
  1362. pet_unlocktarget(pd);
  1363. if(pd->ud.walktimer != INVALID_TIMER && pd->ud.target == sd->bl.id)
  1364. return 0; // Already walking to him
  1365. if (DIFF_TICK(tick, pd->ud.canmove_tick) < 0)
  1366. return 0; // Can't move yet.
  1367. pd->status.speed = (sd->battle_status.speed>>1);
  1368. if(pd->status.speed == 0)
  1369. pd->status.speed = 1;
  1370. if (!unit_walktobl(&pd->bl, &sd->bl, 3, 0))
  1371. pet_randomwalk(pd,tick);
  1372. return 0;
  1373. }
  1374. // Return speed to normal.
  1375. if (pd->status.speed != pd->get_pet_walk_speed()) {
  1376. if (pd->ud.walktimer != INVALID_TIMER)
  1377. return 0; // Wait until the pet finishes walking back to master.
  1378. pd->status.speed = pd->get_pet_walk_speed();
  1379. pd->ud.state.change_walk_target = pd->ud.state.speed_changed = 1;
  1380. }
  1381. if (pd->target_id) {
  1382. target = map_id2bl(pd->target_id);
  1383. if (!target || pd->bl.m != target->m || status_isdead(target) ||
  1384. !check_distance_bl(&pd->bl, target, pd->db->range3)) {
  1385. target = NULL;
  1386. pet_unlocktarget(pd);
  1387. }
  1388. }
  1389. if(!target && pd->loot && pd->loot->count < pd->loot->max && DIFF_TICK(tick,pd->ud.canact_tick) > 0) {
  1390. // Use half the pet's range of sight.
  1391. map_foreachinallrange(pet_ai_sub_hard_lootsearch, &pd->bl, pd->db->range2 / 2, BL_ITEM, pd, &target);
  1392. }
  1393. if (!target) { // Just walk around.
  1394. if (check_distance_bl(&sd->bl, &pd->bl, 3))
  1395. return 0; // Already next to master.
  1396. if(pd->ud.walktimer != INVALID_TIMER && check_distance_blxy(&sd->bl, pd->ud.to_x,pd->ud.to_y, 3))
  1397. return 0; // Already walking to him
  1398. unit_calc_pos(&pd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  1399. if(!unit_walktoxy(&pd->bl,pd->ud.to_x,pd->ud.to_y,0))
  1400. pet_randomwalk(pd,tick);
  1401. return 0;
  1402. }
  1403. if(pd->ud.target == target->id &&
  1404. (pd->ud.attacktimer != INVALID_TIMER || pd->ud.walktimer != INVALID_TIMER))
  1405. return 0; //Target already locked.
  1406. if (target->type != BL_ITEM) { // enemy targeted
  1407. if(!battle_check_range(&pd->bl,target,pd->status.rhw.range)) { // Chase
  1408. if(!unit_walktobl(&pd->bl, target, pd->status.rhw.range, 2))
  1409. pet_unlocktarget(pd); // Unreachable target.
  1410. return 0;
  1411. }
  1412. //Continuous attack.
  1413. unit_attack(&pd->bl, pd->target_id, 1);
  1414. } else { // Item Targeted, attempt loot
  1415. if (!check_distance_bl(&pd->bl, target, 1)) { // Out of range
  1416. if(!unit_walktobl(&pd->bl, target, 1, 1)) // Unreachable target.
  1417. pet_unlocktarget(pd);
  1418. return 0;
  1419. } else {
  1420. struct flooritem_data *fitem = (struct flooritem_data *)target;
  1421. if(pd->loot->count < pd->loot->max) {
  1422. memcpy(&pd->loot->item[pd->loot->count++],&fitem->item,sizeof(pd->loot->item[0]));
  1423. pd->loot->weight += itemdb_weight(fitem->item.nameid)*fitem->item.amount;
  1424. map_clearflooritem(target);
  1425. }
  1426. //Target is unlocked regardless of whether it was picked or not.
  1427. pet_unlocktarget(pd);
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. /**
  1433. * Call pet AI based on targets around.
  1434. * @param sd : player requesting
  1435. * @param ap
  1436. * tick : last search time
  1437. * @return 0
  1438. */
  1439. static int pet_ai_sub_foreachclient(map_session_data *sd,va_list ap)
  1440. {
  1441. t_tick tick = va_arg(ap,t_tick);
  1442. if(sd->status.pet_id && sd->pd)
  1443. pet_ai_sub_hard(sd->pd,sd,tick);
  1444. return 0;
  1445. }
  1446. /**
  1447. * Begin searching for targets.
  1448. * @param tid : (unused)
  1449. * @param tick : last search time
  1450. * @param id : (unused)
  1451. * @param data : data to pass to pet_ai_sub_foreachclient
  1452. * @return 0
  1453. */
  1454. static TIMER_FUNC(pet_ai_hard){
  1455. map_foreachpc(pet_ai_sub_foreachclient,tick);
  1456. return 0;
  1457. }
  1458. /**
  1459. * Make a pet search and grab loot if it can.
  1460. * @param bl : item data
  1461. * @param ap
  1462. * pd : pet requesting
  1463. * target : item
  1464. * @return 1:success, 0:failure
  1465. */
  1466. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  1467. {
  1468. struct pet_data* pd;
  1469. struct flooritem_data *fitem = (struct flooritem_data *)bl;
  1470. struct block_list **target;
  1471. int sd_charid = 0;
  1472. pd = va_arg(ap,struct pet_data *);
  1473. target = va_arg(ap,struct block_list**);
  1474. sd_charid = fitem->first_get_charid;
  1475. if(sd_charid && sd_charid != pd->master->status.char_id)
  1476. return 0;
  1477. if(unit_can_reach_bl(&pd->bl,bl, pd->db->range2, 1, NULL, NULL) &&
  1478. ((*target) == NULL || //New target closer than previous one.
  1479. !check_distance_bl(&pd->bl, *target, distance_bl(&pd->bl, bl)))) {
  1480. (*target) = bl;
  1481. pd->target_id = bl->id;
  1482. return 1;
  1483. }
  1484. return 0;
  1485. }
  1486. /**
  1487. * Make a pet drop their looted items.
  1488. * @param tid : (unused)
  1489. * @param tick : (unused)
  1490. * @param data : items that were looted
  1491. * @return 0
  1492. */
  1493. static TIMER_FUNC(pet_delay_item_drop){
  1494. struct item_drop_list *list;
  1495. struct item_drop *ditem;
  1496. list = (struct item_drop_list *)data;
  1497. ditem = list->item;
  1498. while (ditem) {
  1499. struct item_drop *ditem_prev;
  1500. map_addflooritem(&ditem->item_data,ditem->item_data.amount,
  1501. list->m,list->x,list->y,
  1502. list->first_charid,list->second_charid,list->third_charid,4,0);
  1503. ditem_prev = ditem;
  1504. ditem = ditem->next;
  1505. ers_free(item_drop_ers, ditem_prev);
  1506. }
  1507. ers_free(item_drop_list_ers, list);
  1508. return 0;
  1509. }
  1510. /**
  1511. * Make a pet drop their looted items.
  1512. * @param pd : pet requesting
  1513. * @param sd : player requesting
  1514. * @return 1:success, 0:failure
  1515. */
  1516. int pet_lootitem_drop(struct pet_data *pd,map_session_data *sd)
  1517. {
  1518. int i;
  1519. struct item_drop_list *dlist;
  1520. struct item_drop *ditem;
  1521. if(!pd || !pd->loot || !pd->loot->count)
  1522. return 0;
  1523. dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  1524. dlist->m = pd->bl.m;
  1525. dlist->x = pd->bl.x;
  1526. dlist->y = pd->bl.y;
  1527. dlist->first_charid = 0;
  1528. dlist->second_charid = 0;
  1529. dlist->third_charid = 0;
  1530. dlist->item = NULL;
  1531. for(i = 0; i < pd->loot->count; i++) {
  1532. struct item *it;
  1533. it = &pd->loot->item[i];
  1534. if(sd){
  1535. unsigned char flag = 0;
  1536. if((flag = pc_additem(sd,it,it->amount,LOG_TYPE_PICKDROP_PLAYER))){
  1537. clif_additem(sd,0,0,flag);
  1538. ditem = ers_alloc(item_drop_ers, struct item_drop);
  1539. memcpy(&ditem->item_data, it, sizeof(struct item));
  1540. ditem->next = dlist->item;
  1541. dlist->item = ditem;
  1542. }
  1543. } else {
  1544. ditem = ers_alloc(item_drop_ers, struct item_drop);
  1545. memcpy(&ditem->item_data, it, sizeof(struct item));
  1546. ditem->next = dlist->item;
  1547. dlist->item = ditem;
  1548. }
  1549. }
  1550. //The smart thing to do is use pd->loot->max (thanks for pointing it out, Shinomori)
  1551. memset(pd->loot->item,0,pd->loot->max * sizeof(struct item));
  1552. pd->loot->count = 0;
  1553. pd->loot->weight = 0;
  1554. pd->ud.canact_tick = gettick()+10000; //prevent picked up during 10*1000ms
  1555. if (dlist->item)
  1556. add_timer(gettick()+540,pet_delay_item_drop,0,(intptr_t)dlist);
  1557. else
  1558. ers_free(item_drop_list_ers, dlist);
  1559. return 1;
  1560. }
  1561. /**
  1562. * Pet bonus giving skills.
  1563. * @param tid : current timer value
  1564. * @param tick : next time to use skill
  1565. * @param id : ID of pet owner
  1566. * @author [Valaris], rewritten by [Skotlex]
  1567. */
  1568. TIMER_FUNC(pet_skill_bonus_timer){
  1569. map_session_data *sd = map_id2sd(id);
  1570. struct pet_data *pd;
  1571. int bonus;
  1572. int timer = 0;
  1573. if(sd == NULL || sd->pd==NULL || sd->pd->bonus == NULL)
  1574. return 1;
  1575. pd = sd->pd;
  1576. if(pd->bonus->timer != tid) {
  1577. ShowError("pet_skill_bonus_timer %d != %d\n",pd->bonus->timer,tid);
  1578. pd->bonus->timer = INVALID_TIMER;
  1579. return 0;
  1580. }
  1581. // determine the time for the next timer
  1582. if (pd->state.skillbonus && pd->bonus->delay > 0) {
  1583. bonus = 0;
  1584. timer = pd->bonus->delay*1000; // the duration until pet bonuses will be reactivated again
  1585. } else if (pd->pet.intimate) {
  1586. bonus = 1;
  1587. timer = pd->bonus->duration*1000; // the duration for pet bonuses to be in effect
  1588. } else { //Lost pet...
  1589. pd->bonus->timer = INVALID_TIMER;
  1590. return 0;
  1591. }
  1592. if (pd->state.skillbonus != bonus) {
  1593. pd->state.skillbonus = bonus;
  1594. status_calc_pc(sd,SCO_NONE);
  1595. }
  1596. // wait for the next timer
  1597. pd->bonus->timer=add_timer(tick+timer,pet_skill_bonus_timer,sd->bl.id,0);
  1598. return 0;
  1599. }
  1600. /**
  1601. * Pet recovery skills.
  1602. * @param tid : current timer value
  1603. * @param tick : next time to use skill
  1604. * @param id : ID of pet owner
  1605. * @param data : (unused)
  1606. * @return 0
  1607. * @author [Valaris], rewritten by [Skotlex]
  1608. */
  1609. TIMER_FUNC(pet_recovery_timer){
  1610. map_session_data *sd = map_id2sd(id);
  1611. struct pet_data *pd;
  1612. if(sd == NULL || sd->pd == NULL || sd->pd->recovery == NULL)
  1613. return 1;
  1614. pd = sd->pd;
  1615. if(pd->recovery->timer != tid) {
  1616. ShowError("pet_recovery_timer %d != %d\n",pd->recovery->timer,tid);
  1617. return 0;
  1618. }
  1619. if(sd->sc.getSCE(pd->recovery->type)) {
  1620. //Display a heal animation?
  1621. //Detoxify is chosen for now.
  1622. clif_skill_nodamage(&pd->bl,&sd->bl,TF_DETOXIFY,1,1);
  1623. status_change_end(&sd->bl, pd->recovery->type);
  1624. clif_emotion(&pd->bl, ET_OK);
  1625. }
  1626. pd->recovery->timer = INVALID_TIMER;
  1627. return 0;
  1628. }
  1629. /**
  1630. * Pet natural regeneration timer.
  1631. * @param tid : current timer value
  1632. * @param tick : next time to regenerate
  1633. * @param id : ID of pet owner
  1634. */
  1635. TIMER_FUNC(pet_heal_timer){
  1636. map_session_data *sd = map_id2sd(id);
  1637. struct status_data *status;
  1638. struct pet_data *pd;
  1639. unsigned int rate = 100;
  1640. if(sd == NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1641. return 1;
  1642. pd = sd->pd;
  1643. if(pd->s_skill->timer != tid) {
  1644. ShowError("pet_heal_timer %d != %d\n",pd->s_skill->timer,tid);
  1645. return 0;
  1646. }
  1647. status = status_get_status_data(&sd->bl);
  1648. if(pc_isdead(sd) ||
  1649. (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp ||
  1650. (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp ||
  1651. (rate = (pd->ud.skilltimer != INVALID_TIMER)) //Another skill is in effect
  1652. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1653. pd->s_skill->timer=add_timer(gettick()+(rate>10?rate:10)*100,pet_heal_timer,sd->bl.id,0);
  1654. return 0;
  1655. }
  1656. pet_stop_attack(pd);
  1657. pet_stop_walking(pd,1);
  1658. clif_skill_nodamage(&pd->bl,&sd->bl,AL_HEAL,pd->s_skill->lv,1);
  1659. status_heal(&sd->bl, pd->s_skill->lv,0, 0);
  1660. pd->s_skill->timer = add_timer(tick+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  1661. return 0;
  1662. }
  1663. /**
  1664. * Pet support skills.
  1665. * @param tid : current timer value
  1666. * @param tick : next time to use skill
  1667. * @param id : ID of pet owner
  1668. * @param data : (unused)
  1669. * @author [Skotlex]
  1670. */
  1671. TIMER_FUNC(pet_skill_support_timer){
  1672. map_session_data *sd = map_id2sd(id);
  1673. struct pet_data *pd;
  1674. struct status_data *status;
  1675. short rate = 100;
  1676. if(sd == NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1677. return 1;
  1678. pd = sd->pd;
  1679. if(pd->s_skill->timer != tid) {
  1680. ShowError("pet_skill_support_timer %d != %d\n",pd->s_skill->timer,tid);
  1681. return 0;
  1682. }
  1683. status = status_get_status_data(&sd->bl);
  1684. if (DIFF_TICK(pd->ud.canact_tick, tick) > 0) {
  1685. //Wait until the pet can act again.
  1686. pd->s_skill->timer=add_timer(pd->ud.canact_tick,pet_skill_support_timer,sd->bl.id,0);
  1687. return 0;
  1688. }
  1689. if(pc_isdead(sd) ||
  1690. (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp ||
  1691. (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp ||
  1692. (rate = (pd->ud.skilltimer != INVALID_TIMER)) //Another skill is in effect
  1693. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1694. pd->s_skill->timer=add_timer(tick+(rate>10?rate:10)*100,pet_skill_support_timer,sd->bl.id,0);
  1695. return 0;
  1696. }
  1697. pet_stop_attack(pd);
  1698. pet_stop_walking(pd,1);
  1699. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  1700. if (skill_get_inf(pd->s_skill->id) & INF_GROUND_SKILL)
  1701. unit_skilluse_pos(&pd->bl, sd->bl.x, sd->bl.y, pd->s_skill->id, pd->s_skill->lv);
  1702. else
  1703. unit_skilluse_id(&pd->bl, sd->bl.id, pd->s_skill->id, pd->s_skill->lv);
  1704. return 0;
  1705. }
  1706. /**
  1707. * Search for the egg of pet pointed by pd from the player's inventory
  1708. * @param sd : player
  1709. * @param pet_id : pet ID of the pet
  1710. * @return index of egg in player's inventory or -1 if the egg is not found.
  1711. */
  1712. int pet_egg_search(map_session_data* sd, int pet_id) {
  1713. for (int i = 0; i < MAX_INVENTORY; i++) {
  1714. if (sd->inventory.u.items_inventory[i].card[0] == CARD0_PET &&
  1715. pet_id == MakeDWord(sd->inventory.u.items_inventory[i].card[1], sd->inventory.u.items_inventory[i].card[2]))
  1716. return i;
  1717. }
  1718. return -1;
  1719. }
  1720. /**
  1721. * Check if the pet owner has the required evolution items
  1722. * @param sd: Player requesting the evolution item check
  1723. * @param pet_id: Pet's database ID
  1724. * @return True on success or false otherwise
  1725. */
  1726. bool pet_evolution_requirements_check(map_session_data *sd, short pet_id) {
  1727. nullpo_retr(false, sd);
  1728. if (sd->pd == nullptr)
  1729. return false;
  1730. auto pet_db_ptr = sd->pd->get_pet_db();
  1731. auto evo_data = pet_db_ptr->evolution_data.find(pet_id);
  1732. if (evo_data == pet_db_ptr->evolution_data.end()) {
  1733. return false;
  1734. }
  1735. for (const auto &requirement : evo_data->second->requirements) {
  1736. int count = 0;
  1737. for (int i = 0; i < MAX_INVENTORY; i++) {
  1738. if (sd->inventory.u.items_inventory[i].nameid == requirement.first) {
  1739. count += sd->inventory.u.items_inventory[i].amount;
  1740. }
  1741. }
  1742. if (count < requirement.second)
  1743. return false;
  1744. }
  1745. return true;
  1746. }
  1747. /**
  1748. * Process a pet evolution request
  1749. * @param sd: Player requesting the evolution
  1750. * @param pet_id: Pet's database ID
  1751. */
  1752. void pet_evolution(map_session_data *sd, int16 pet_id) {
  1753. nullpo_retv(sd);
  1754. if (sd->pd == nullptr) {
  1755. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_UNKNOWN);
  1756. return;
  1757. }
  1758. if (!battle_config.feature_petevolution) {
  1759. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_UNKNOWN);
  1760. return;
  1761. }
  1762. if (sd->pd->pet.intimate < PET_INTIMATE_LOYAL) {
  1763. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_RG_FAMILIAR);
  1764. return;
  1765. }
  1766. if (sd->pd->pet.equip) {
  1767. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_RG_FAMILIAR);
  1768. return;
  1769. }
  1770. auto pet_db_ptr = sd->pd->get_pet_db();
  1771. if (pet_db_ptr->evolution_data.find(pet_id) == pet_db_ptr->evolution_data.end()) {
  1772. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOTEXIST_CALLPET);
  1773. return;
  1774. }
  1775. if (!pet_evolution_requirements_check(sd, pet_id)) {
  1776. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_MATERIAL);
  1777. return;
  1778. }
  1779. for (const auto &requirement : pet_db_ptr->evolution_data[pet_id]->requirements) {
  1780. int count = requirement.second;
  1781. for (int i = 0; i < MAX_INVENTORY; i++) {
  1782. item *slot = &sd->inventory.u.items_inventory[i];
  1783. int deduction = min(requirement.second, slot->amount);
  1784. if (slot->nameid == requirement.first) {
  1785. pc_delitem(sd, i, deduction, 0, 0, LOG_TYPE_OTHER);
  1786. count -= deduction;
  1787. if (count == 0)
  1788. break;
  1789. }
  1790. }
  1791. }
  1792. std::shared_ptr<s_pet_db> new_data = pet_db.find(pet_id);
  1793. if( new_data == nullptr ){
  1794. return;
  1795. }
  1796. int idx = pet_egg_search(sd, sd->pd->pet.pet_id);
  1797. if( idx == -1 ){
  1798. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOTEXIST_CALLPET);
  1799. return;
  1800. }
  1801. // Virtually delete the old egg
  1802. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx]);
  1803. clif_delitem(sd, idx, 1, 0);
  1804. // Change the old egg to the new one
  1805. sd->inventory.u.items_inventory[idx].nameid = new_data->EggID;
  1806. sd->inventory_data[idx] = itemdb_search(new_data->EggID);
  1807. // Virtually add it to the inventory
  1808. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx]);
  1809. clif_additem(sd, idx, 1, 0);
  1810. // Remove the old pet from sight
  1811. unit_remove_map(&sd->pd->bl, CLR_OUTSIGHT);
  1812. // Prepare the new pet
  1813. sd->pd->pet.class_ = pet_id;
  1814. sd->pd->pet.egg_id = new_data->EggID;
  1815. pet_set_intimate(sd->pd, new_data->intimate);
  1816. if( !sd->pd->pet.rename_flag ){
  1817. std::shared_ptr<s_mob_db> mdb = mob_db.find( pet_id );
  1818. safestrncpy(sd->pd->pet.name, mdb->jname.c_str(), NAME_LENGTH);
  1819. }
  1820. status_set_viewdata(&sd->pd->bl, pet_id);
  1821. // Save the pet and inventory data
  1822. intif_save_petdata(sd->status.account_id, &sd->pd->pet);
  1823. if (save_settings&CHARSAVE_PET)
  1824. chrif_save(sd, CSAVE_INVENTORY);
  1825. // Spawn it
  1826. if (map_addblock(&sd->pd->bl))
  1827. return;
  1828. clif_spawn(&sd->pd->bl);
  1829. clif_send_petdata(sd, sd->pd, 0, 0);
  1830. clif_send_petdata(sd, sd->pd, 5, battle_config.pet_hair_style);
  1831. clif_pet_equip_area(sd->pd);
  1832. clif_send_petstatus(sd);
  1833. clif_emotion(&sd->bl, ET_BEST);
  1834. clif_specialeffect(&sd->pd->bl, EF_HO_UP, AREA);
  1835. clif_pet_evolution_result(sd, e_pet_evolution_result::SUCCESS);
  1836. clif_inventorylist(sd);
  1837. }
  1838. /**
  1839. * Add petautobonus to player when attacking/attacked.
  1840. * @param bonus: Bonus
  1841. * @param script: Script to execute
  1842. * @param rate: Success chance
  1843. * @param dur: Duration
  1844. * @param flag: Battle flag/skill
  1845. * @param other_script: Secondary script to execute
  1846. * @param onskill: Skill used to trigger autobonus
  1847. * @return True on success or false otherwise
  1848. */
  1849. bool pet_addautobonus(std::vector<std::shared_ptr<s_petautobonus>> &bonus, const std::string &script, int16 rate, uint32 dur, uint16 flag, const std::string &other_script, bool onskill) {
  1850. // Check if the same bonus already exists
  1851. for (auto &autobonus : bonus) {
  1852. // Compare based on bonus script
  1853. if (script == autobonus->bonus_script) {
  1854. return false;
  1855. }
  1856. }
  1857. if (bonus.size() == MAX_PC_BONUS) {
  1858. ShowWarning("pet_addautobonus: Reached max (%d) number of petautobonus per pet!\n", MAX_PC_BONUS);
  1859. return false;
  1860. }
  1861. if (!onskill) {
  1862. if (!(flag & BF_RANGEMASK))
  1863. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1864. if (!(flag & BF_WEAPONMASK))
  1865. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1866. if (!(flag & BF_SKILLMASK)) {
  1867. if (flag & (BF_MAGIC | BF_MISC))
  1868. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1869. if (flag & BF_WEAPON)
  1870. flag |= BF_NORMAL | BF_SKILL;
  1871. }
  1872. }
  1873. if (rate < -10000 || rate > 10000)
  1874. ShowWarning("pet_addautobonus: Bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1875. std::shared_ptr<s_petautobonus> entry = std::make_shared<s_petautobonus>();
  1876. entry->rate = cap_value(rate, -10000, 10000);
  1877. entry->duration = dur;
  1878. entry->timer = INVALID_TIMER;
  1879. entry->atk_type = flag;
  1880. entry->bonus_script = script;
  1881. entry->other_script = other_script;
  1882. bonus.push_back(entry);
  1883. return true;
  1884. }
  1885. /**
  1886. * Remove a petautobonus from player.
  1887. * @param sd: Player data
  1888. * @param bonus: Autobonus
  1889. * @param restore: Run script on clearing or not
  1890. */
  1891. void pet_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_petautobonus>> &bonus, bool restore) {
  1892. std::vector<std::shared_ptr<s_petautobonus>>::iterator it = bonus.begin();
  1893. while (it != bonus.end()) {
  1894. std::shared_ptr<s_petautobonus> b = *it;
  1895. if (b->timer != INVALID_TIMER && !b->bonus_script.empty() && restore) {
  1896. script_run_petautobonus(b->bonus_script, sd);
  1897. }
  1898. if (restore) {
  1899. it++;
  1900. continue;
  1901. }
  1902. it = bonus.erase(it);
  1903. }
  1904. }
  1905. /**
  1906. * Execute petautobonus on player.
  1907. * @param sd: Player data
  1908. * @param bonus: Bonus vector
  1909. * @param autobonus: Autobonus to run
  1910. */
  1911. void pet_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_petautobonus>> *bonus, std::shared_ptr<s_petautobonus> &autobonus) {
  1912. if (autobonus->timer != INVALID_TIMER)
  1913. delete_timer(autobonus->timer, pet_endautobonus);
  1914. if (!autobonus->other_script.empty()) {
  1915. script_run_petautobonus(autobonus->other_script, sd);
  1916. }
  1917. autobonus->timer = add_timer(gettick() + autobonus->duration, pet_endautobonus, sd.bl.id, (intptr_t)bonus);
  1918. status_calc_pc(&sd, SCO_FORCE);
  1919. }
  1920. /**
  1921. * Remove petautobonus timer from player.
  1922. */
  1923. TIMER_FUNC(pet_endautobonus) {
  1924. map_session_data *sd = map_id2sd(id);
  1925. std::vector<std::shared_ptr<s_petautobonus>> *bonus = (std::vector<std::shared_ptr<s_petautobonus>> *)data;
  1926. nullpo_ret(sd);
  1927. nullpo_ret(bonus);
  1928. for (std::shared_ptr<s_petautobonus> autobonus : *bonus) {
  1929. if (autobonus->timer == tid) {
  1930. autobonus->timer = INVALID_TIMER;
  1931. break;
  1932. }
  1933. }
  1934. status_calc_pc(sd, SCO_FORCE);
  1935. return 0;
  1936. }
  1937. /**
  1938. * Initialize pet data.
  1939. */
  1940. void do_init_pet(void)
  1941. {
  1942. pet_db.load();
  1943. item_drop_ers = ers_new(sizeof(struct item_drop),"pet.cpp::item_drop_ers",ERS_OPT_NONE);
  1944. item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"pet.cpp::item_drop_list_ers",ERS_OPT_NONE);
  1945. add_timer_func_list(pet_hungry,"pet_hungry");
  1946. add_timer_func_list(pet_ai_hard,"pet_ai_hard");
  1947. add_timer_func_list(pet_skill_bonus_timer,"pet_skill_bonus_timer"); // [Valaris]
  1948. add_timer_func_list(pet_delay_item_drop,"pet_delay_item_drop");
  1949. add_timer_func_list(pet_skill_support_timer, "pet_skill_support_timer"); // [Skotlex]
  1950. add_timer_func_list(pet_recovery_timer,"pet_recovery_timer"); // [Valaris]
  1951. add_timer_func_list(pet_heal_timer,"pet_heal_timer"); // [Valaris]
  1952. add_timer_func_list(pet_endautobonus, "pet_endautobonus");
  1953. add_timer_interval(gettick()+MIN_PETTHINKTIME,pet_ai_hard,0,0,MIN_PETTHINKTIME);
  1954. }
  1955. /**
  1956. * Destroy pet data.
  1957. */
  1958. void do_final_pet(void)
  1959. {
  1960. ers_destroy(item_drop_ers);
  1961. ers_destroy(item_drop_list_ers);
  1962. pet_autobonuses.clear();
  1963. pet_db.clear();
  1964. }