BeastFarm.java 16 KB

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