BeastFarm.java 16 KB

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