BeastFarm.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package ai.group_template;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import quests.Q00020_BringUpWithLove.Q00020_BringUpWithLove;
  26. import ai.npc.AbstractNpcAI;
  27. import com.l2jserver.gameserver.ai.CtrlIntention;
  28. import com.l2jserver.gameserver.datatables.SkillData;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Attackable;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2TamedBeastInstance;
  34. import com.l2jserver.gameserver.model.holders.SkillHolder;
  35. import com.l2jserver.gameserver.model.skills.Skill;
  36. import com.l2jserver.gameserver.network.serverpackets.AbstractNpcInfo;
  37. import com.l2jserver.gameserver.util.Util;
  38. /**
  39. * Growth-capable mobs: Polymorphing upon successful feeding.<br>
  40. * Updated to Freya.
  41. * @author Fulminus, Gigiikun
  42. */
  43. public final class BeastFarm extends AbstractNpcAI
  44. {
  45. private static final int GOLDEN_SPICE = 15474;
  46. private static final int CRYSTAL_SPICE = 15475;
  47. private static final int SKILL_GOLDEN_SPICE = 9049;
  48. private static final int SKILL_CRYSTAL_SPICE = 9050;
  49. private static final int SKILL_BLESSED_GOLDEN_SPICE = 9051;
  50. private static final int SKILL_BLESSED_CRYSTAL_SPICE = 9052;
  51. private static final int SKILL_SGRADE_GOLDEN_SPICE = 9053;
  52. private static final int SKILL_SGRADE_CRYSTAL_SPICE = 9054;
  53. private static final int[] TAMED_BEASTS =
  54. {
  55. 18869,
  56. 18870,
  57. 18871,
  58. 18872
  59. };
  60. private static final int TAME_CHANCE = 20;
  61. protected static final int[] SPECIAL_SPICE_CHANCES =
  62. {
  63. 33,
  64. 75
  65. };
  66. // all mobs that can eat...
  67. private static final int[] FEEDABLE_BEASTS =
  68. {
  69. // Kookaburras
  70. 18873,
  71. 18874,
  72. 18875,
  73. 18876,
  74. 18877,
  75. 18878,
  76. 18879,
  77. // Cougars
  78. 18880,
  79. 18881,
  80. 18882,
  81. 18883,
  82. 18884,
  83. 18885,
  84. 18886,
  85. // Buffalos
  86. 18887,
  87. 18888,
  88. 18889,
  89. 18890,
  90. 18891,
  91. 18892,
  92. 18893,
  93. // Grendels
  94. 18894,
  95. 18895,
  96. 18896,
  97. 18897,
  98. 18898,
  99. 18899,
  100. 18900
  101. };
  102. private static final Map<Integer, Integer> FEED_INFO = new ConcurrentHashMap<>();
  103. private static final Map<Integer, GrowthCapableMob> GROWTH_CAPABLE_MONSTERS = new HashMap<>();
  104. private static List<TamedBeast> TAMED_BEAST_DATA = new ArrayList<>();
  105. private BeastFarm()
  106. {
  107. super(BeastFarm.class.getSimpleName(), "ai/group_template");
  108. addSkillSeeId(FEEDABLE_BEASTS);
  109. addKillId(FEEDABLE_BEASTS);
  110. GrowthCapableMob temp;
  111. // Kookabura
  112. temp = new GrowthCapableMob(100, 0, 18869);
  113. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18874);
  114. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18875);
  115. temp.addNpcIdForSkillId(SKILL_BLESSED_GOLDEN_SPICE, 18869);
  116. temp.addNpcIdForSkillId(SKILL_BLESSED_CRYSTAL_SPICE, 18869);
  117. temp.addNpcIdForSkillId(SKILL_SGRADE_GOLDEN_SPICE, 18878);
  118. temp.addNpcIdForSkillId(SKILL_SGRADE_CRYSTAL_SPICE, 18879);
  119. GROWTH_CAPABLE_MONSTERS.put(18873, temp);
  120. temp = new GrowthCapableMob(40, 1, 18869);
  121. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18876);
  122. GROWTH_CAPABLE_MONSTERS.put(18874, temp);
  123. temp = new GrowthCapableMob(40, 1, 18869);
  124. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18877);
  125. GROWTH_CAPABLE_MONSTERS.put(18875, temp);
  126. temp = new GrowthCapableMob(25, 2, 18869);
  127. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18878);
  128. GROWTH_CAPABLE_MONSTERS.put(18876, temp);
  129. temp = new GrowthCapableMob(25, 2, 18869);
  130. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18879);
  131. GROWTH_CAPABLE_MONSTERS.put(18877, temp);
  132. // Cougar
  133. temp = new GrowthCapableMob(100, 0, 18870);
  134. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18881);
  135. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18882);
  136. temp.addNpcIdForSkillId(SKILL_BLESSED_GOLDEN_SPICE, 18870);
  137. temp.addNpcIdForSkillId(SKILL_BLESSED_CRYSTAL_SPICE, 18870);
  138. temp.addNpcIdForSkillId(SKILL_SGRADE_GOLDEN_SPICE, 18885);
  139. temp.addNpcIdForSkillId(SKILL_SGRADE_CRYSTAL_SPICE, 18886);
  140. GROWTH_CAPABLE_MONSTERS.put(18880, temp);
  141. temp = new GrowthCapableMob(40, 1, 18870);
  142. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18883);
  143. GROWTH_CAPABLE_MONSTERS.put(18881, temp);
  144. temp = new GrowthCapableMob(40, 1, 18870);
  145. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18884);
  146. GROWTH_CAPABLE_MONSTERS.put(18882, temp);
  147. temp = new GrowthCapableMob(25, 2, 18870);
  148. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18885);
  149. GROWTH_CAPABLE_MONSTERS.put(18883, temp);
  150. temp = new GrowthCapableMob(25, 2, 18870);
  151. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18886);
  152. GROWTH_CAPABLE_MONSTERS.put(18884, temp);
  153. // Buffalo
  154. temp = new GrowthCapableMob(100, 0, 18871);
  155. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18888);
  156. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18889);
  157. temp.addNpcIdForSkillId(SKILL_BLESSED_GOLDEN_SPICE, 18871);
  158. temp.addNpcIdForSkillId(SKILL_BLESSED_CRYSTAL_SPICE, 18871);
  159. temp.addNpcIdForSkillId(SKILL_SGRADE_GOLDEN_SPICE, 18892);
  160. temp.addNpcIdForSkillId(SKILL_SGRADE_CRYSTAL_SPICE, 18893);
  161. GROWTH_CAPABLE_MONSTERS.put(18887, temp);
  162. temp = new GrowthCapableMob(40, 1, 18871);
  163. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18890);
  164. GROWTH_CAPABLE_MONSTERS.put(18888, temp);
  165. temp = new GrowthCapableMob(40, 1, 18871);
  166. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18891);
  167. GROWTH_CAPABLE_MONSTERS.put(18889, temp);
  168. temp = new GrowthCapableMob(25, 2, 18871);
  169. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18892);
  170. GROWTH_CAPABLE_MONSTERS.put(18890, temp);
  171. temp = new GrowthCapableMob(25, 2, 18871);
  172. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18893);
  173. GROWTH_CAPABLE_MONSTERS.put(18891, temp);
  174. // Grendel
  175. temp = new GrowthCapableMob(100, 0, 18872);
  176. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18895);
  177. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18896);
  178. temp.addNpcIdForSkillId(SKILL_BLESSED_GOLDEN_SPICE, 18872);
  179. temp.addNpcIdForSkillId(SKILL_BLESSED_CRYSTAL_SPICE, 18872);
  180. temp.addNpcIdForSkillId(SKILL_SGRADE_GOLDEN_SPICE, 18899);
  181. temp.addNpcIdForSkillId(SKILL_SGRADE_CRYSTAL_SPICE, 18900);
  182. GROWTH_CAPABLE_MONSTERS.put(18894, temp);
  183. temp = new GrowthCapableMob(40, 1, 18872);
  184. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18897);
  185. GROWTH_CAPABLE_MONSTERS.put(18895, temp);
  186. temp = new GrowthCapableMob(40, 1, 18872);
  187. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18898);
  188. GROWTH_CAPABLE_MONSTERS.put(18896, temp);
  189. temp = new GrowthCapableMob(25, 2, 18872);
  190. temp.addNpcIdForSkillId(SKILL_GOLDEN_SPICE, 18899);
  191. GROWTH_CAPABLE_MONSTERS.put(18897, temp);
  192. temp = new GrowthCapableMob(25, 2, 18872);
  193. temp.addNpcIdForSkillId(SKILL_CRYSTAL_SPICE, 18900);
  194. GROWTH_CAPABLE_MONSTERS.put(18898, temp);
  195. // Tamed beasts data
  196. TAMED_BEAST_DATA.add(new TamedBeast("%name% of Focus", new SkillHolder(6432, 1), new SkillHolder(6668, 1)));
  197. TAMED_BEAST_DATA.add(new TamedBeast("%name% of Guiding", new SkillHolder(6433, 1), new SkillHolder(6670, 1)));
  198. TAMED_BEAST_DATA.add(new TamedBeast("%name% of Swifth", new SkillHolder(6434, 1), new SkillHolder(6667, 1)));
  199. TAMED_BEAST_DATA.add(new TamedBeast("Berserker %name%", new SkillHolder(6671, 1)));
  200. TAMED_BEAST_DATA.add(new TamedBeast("%name% of Protect", new SkillHolder(6669, 1), new SkillHolder(6672, 1)));
  201. TAMED_BEAST_DATA.add(new TamedBeast("%name% of Vigor", new SkillHolder(6431, 1), new SkillHolder(6666, 1)));
  202. }
  203. public void spawnNext(L2Npc npc, L2PcInstance player, int nextNpcId, int food)
  204. {
  205. // remove the feedinfo of the mob that got despawned, if any
  206. if (FEED_INFO.containsKey(npc.getObjectId()))
  207. {
  208. if (FEED_INFO.get(npc.getObjectId()) == player.getObjectId())
  209. {
  210. FEED_INFO.remove(npc.getObjectId());
  211. }
  212. }
  213. // despawn the old mob
  214. // TODO: same code? FIXED?
  215. /*
  216. * if (_GrowthCapableMobs.get(npc.getNpcId()).getGrowthLevel() == 0) { npc.deleteMe(); } else {
  217. */
  218. npc.deleteMe();
  219. // }
  220. // if this is finally a trained mob, then despawn any other trained mobs that the
  221. // player might have and initialize the Tamed Beast.
  222. if (Util.contains(TAMED_BEASTS, nextNpcId))
  223. {
  224. final L2TamedBeastInstance nextNpc = new L2TamedBeastInstance(nextNpcId, player, food, npc.getX(), npc.getY(), npc.getZ(), true);
  225. TamedBeast beast = TAMED_BEAST_DATA.get(getRandom(TAMED_BEAST_DATA.size()));
  226. String name = beast.getName();
  227. switch (nextNpcId)
  228. {
  229. case 18869:
  230. name = name.replace("%name%", "Alpine Kookaburra");
  231. break;
  232. case 18870:
  233. name = name.replace("%name%", "Alpine Cougar");
  234. break;
  235. case 18871:
  236. name = name.replace("%name%", "Alpine Buffalo");
  237. break;
  238. case 18872:
  239. name = name.replace("%name%", "Alpine Grendel");
  240. break;
  241. }
  242. nextNpc.setName(name);
  243. nextNpc.broadcastPacket(new AbstractNpcInfo.NpcInfo(nextNpc, player));
  244. nextNpc.setRunning();
  245. SkillData st = SkillData.getInstance();
  246. for (SkillHolder sh : beast.getSkills())
  247. {
  248. nextNpc.addBeastSkill(st.getSkill(sh.getSkillId(), sh.getSkillLvl()));
  249. }
  250. Q00020_BringUpWithLove.checkJewelOfInnocence(player);
  251. }
  252. else
  253. {
  254. // if not trained, the newly spawned mob will automatically be agro against its feeder
  255. // (what happened to "never bite the hand that feeds you" anyway?!)
  256. L2Attackable nextNpc = (L2Attackable) addSpawn(nextNpcId, npc);
  257. // register the player in the feedinfo for the mob that just spawned
  258. FEED_INFO.put(nextNpc.getObjectId(), player.getObjectId());
  259. nextNpc.setRunning();
  260. nextNpc.addDamageHate(player, 0, 99999);
  261. nextNpc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, player);
  262. player.setTarget(nextNpc);
  263. }
  264. }
  265. @Override
  266. public String onSkillSee(L2Npc npc, L2PcInstance caster, Skill skill, L2Object[] targets, boolean isSummon)
  267. {
  268. // this behavior is only run when the target of skill is the passed npc (chest)
  269. // i.e. when the player is attempting to open the chest using a skill
  270. if (!Util.contains(targets, npc))
  271. {
  272. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  273. }
  274. // gather some values on local variables
  275. int npcId = npc.getId();
  276. int skillId = skill.getId();
  277. // check if the npc and skills used are valid for this script. Exit if invalid.
  278. if (!Util.contains(FEEDABLE_BEASTS, npcId) || ((skillId != SKILL_GOLDEN_SPICE) && (skillId != SKILL_CRYSTAL_SPICE) && (skillId != SKILL_BLESSED_GOLDEN_SPICE) && (skillId != SKILL_BLESSED_CRYSTAL_SPICE) && (skillId != SKILL_SGRADE_GOLDEN_SPICE) && (skillId != SKILL_SGRADE_CRYSTAL_SPICE)))
  279. {
  280. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  281. }
  282. // first gather some values on local variables
  283. int objectId = npc.getObjectId();
  284. int growthLevel = 3; // if a mob is in FEEDABLE_BEASTS but not in _GrowthCapableMobs, then it's at max growth (3)
  285. if (GROWTH_CAPABLE_MONSTERS.containsKey(npcId))
  286. {
  287. growthLevel = GROWTH_CAPABLE_MONSTERS.get(npcId).getGrowthLevel();
  288. }
  289. // prevent exploit which allows 2 players to simultaneously raise the same 0-growth beast
  290. // If the mob is at 0th level (when it still listens to all feeders) lock it to the first feeder!
  291. if ((growthLevel == 0) && FEED_INFO.containsKey(objectId))
  292. {
  293. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  294. }
  295. FEED_INFO.put(objectId, caster.getObjectId());
  296. // display the social action of the beast eating the food.
  297. npc.broadcastSocialAction(2);
  298. int food = 0;
  299. if ((skillId == SKILL_GOLDEN_SPICE) || (skillId == SKILL_BLESSED_GOLDEN_SPICE))
  300. {
  301. food = GOLDEN_SPICE;
  302. }
  303. else if ((skillId == SKILL_CRYSTAL_SPICE) || (skillId == SKILL_BLESSED_CRYSTAL_SPICE))
  304. {
  305. food = CRYSTAL_SPICE;
  306. }
  307. // if this pet can't grow, it's all done.
  308. if (GROWTH_CAPABLE_MONSTERS.containsKey(npcId))
  309. {
  310. // do nothing if this mob doesn't eat the specified food (food gets consumed but has no effect).
  311. int newNpcId = GROWTH_CAPABLE_MONSTERS.get(npcId).getLeveledNpcId(skillId);
  312. if (newNpcId == -1)
  313. {
  314. if (growthLevel == 0)
  315. {
  316. FEED_INFO.remove(objectId);
  317. npc.setRunning();
  318. ((L2Attackable) npc).addDamageHate(caster, 0, 1);
  319. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, caster);
  320. }
  321. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  322. }
  323. else if ((growthLevel > 0) && (FEED_INFO.get(objectId) != caster.getObjectId()))
  324. {
  325. // check if this is the same player as the one who raised it from growth 0.
  326. // if no, then do not allow a chance to raise the pet (food gets consumed but has no effect).
  327. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  328. }
  329. spawnNext(npc, caster, newNpcId, food);
  330. }
  331. else
  332. {
  333. caster.sendMessage("The beast spit out the feed instead of eating it.");
  334. npc.dropItem(caster, food, 1);
  335. }
  336. return super.onSkillSee(npc, caster, skill, targets, isSummon);
  337. }
  338. @Override
  339. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  340. {
  341. // remove the feedinfo of the mob that got killed, if any
  342. if (FEED_INFO.containsKey(npc.getObjectId()))
  343. {
  344. FEED_INFO.remove(npc.getObjectId());
  345. }
  346. return super.onKill(npc, killer, isSummon);
  347. }
  348. // all mobs that grow by eating
  349. private static class GrowthCapableMob
  350. {
  351. private final int _chance;
  352. private final int _growthLevel;
  353. private final int _tameNpcId;
  354. private final Map<Integer, Integer> _skillSuccessNpcIdList = new HashMap<>();
  355. public GrowthCapableMob(int chance, int growthLevel, int tameNpcId)
  356. {
  357. _chance = chance;
  358. _growthLevel = growthLevel;
  359. _tameNpcId = tameNpcId;
  360. }
  361. public void addNpcIdForSkillId(int skillId, int npcId)
  362. {
  363. _skillSuccessNpcIdList.put(skillId, npcId);
  364. }
  365. public int getGrowthLevel()
  366. {
  367. return _growthLevel;
  368. }
  369. public int getLeveledNpcId(int skillId)
  370. {
  371. if (!_skillSuccessNpcIdList.containsKey(skillId))
  372. {
  373. return -1;
  374. }
  375. else if ((skillId == SKILL_BLESSED_GOLDEN_SPICE) || (skillId == SKILL_BLESSED_CRYSTAL_SPICE) || (skillId == SKILL_SGRADE_GOLDEN_SPICE) || (skillId == SKILL_SGRADE_CRYSTAL_SPICE))
  376. {
  377. if (getRandom(100) < SPECIAL_SPICE_CHANCES[0])
  378. {
  379. if (getRandom(100) < SPECIAL_SPICE_CHANCES[1])
  380. {
  381. return _skillSuccessNpcIdList.get(skillId);
  382. }
  383. else if ((skillId == SKILL_BLESSED_GOLDEN_SPICE) || (skillId == SKILL_SGRADE_GOLDEN_SPICE))
  384. {
  385. return _skillSuccessNpcIdList.get(SKILL_GOLDEN_SPICE);
  386. }
  387. else
  388. {
  389. return _skillSuccessNpcIdList.get(SKILL_CRYSTAL_SPICE);
  390. }
  391. }
  392. return -1;
  393. }
  394. else if ((_growthLevel == 2) && (getRandom(100) < TAME_CHANCE))
  395. {
  396. return _tameNpcId;
  397. }
  398. else if (getRandom(100) < _chance)
  399. {
  400. return _skillSuccessNpcIdList.get(skillId);
  401. }
  402. else
  403. {
  404. return -1;
  405. }
  406. }
  407. }
  408. private static class TamedBeast
  409. {
  410. private final String name;
  411. private final SkillHolder[] sh;
  412. public TamedBeast(String beastName, SkillHolder... holders)
  413. {
  414. name = beastName;
  415. sh = holders;
  416. }
  417. public String getName()
  418. {
  419. return name;
  420. }
  421. public SkillHolder[] getSkills()
  422. {
  423. return sh;
  424. }
  425. }
  426. public static void main(String[] args)
  427. {
  428. new BeastFarm();
  429. }
  430. }