BeastFarm.java 17 KB

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