RuneHive-Game
Loading...
Searching...
No Matches
PlayerMagicStrategy.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat.strategy.player;
2
3import com.runehive.content.itemaction.impl.ThammaronsSceptre;
4import com.runehive.game.Animation;
5import com.runehive.game.Graphic;
6import com.runehive.game.UpdatePriority;
7import com.runehive.game.world.entity.combat.CombatImpact;
8import com.runehive.game.world.entity.combat.CombatType;
9import com.runehive.game.world.entity.combat.attack.FightType;
10import com.runehive.game.world.entity.combat.hit.CombatHit;
11import com.runehive.game.world.entity.combat.hit.Hit;
12import com.runehive.game.world.entity.combat.magic.CombatSpell;
13import com.runehive.game.world.entity.combat.magic.MagicRune;
14import com.runehive.game.world.entity.combat.strategy.basic.MagicStrategy;
15import com.runehive.game.world.entity.mob.Mob;
16import com.runehive.game.world.entity.mob.player.Player;
17import com.runehive.game.world.entity.mob.player.PlayerRight;
18import com.runehive.game.world.entity.skill.Skill;
19import com.runehive.game.world.items.Item;
20import com.runehive.game.world.items.containers.equipment.Equipment;
21import com.runehive.net.packet.out.SendMessage;
22import com.runehive.util.RandomUtils;
23
24import java.util.Collections;
25import java.util.LinkedList;
26import java.util.List;
27import java.util.function.Consumer;
28import java.util.function.Predicate;
29
30import static com.runehive.game.world.items.containers.equipment.Equipment.WEAPON_SLOT;
31
32public class PlayerMagicStrategy extends MagicStrategy<Player> {
33
34 /** The magic spell definition. */
35 private final CombatSpell spell;
36
37 /** Constructs a new {@code SpellStrategy} from a {@link CombatSpell}. */
39 this.spell = spell;
40 }
41
42 @Override
43 public boolean canAttack(Player attacker, Mob defender) {
44 if (defender.isPlayer() && defender.getPlayer().isBot && !PlayerRight.isOwner(defender.getPlayer())) {
45 attacker.message("You can't attack bots with magic.");
46 return false;
47 }
48
50 if (defender.isPlayer()) {
51 if (defender.getPlayer().isTeleblocked()) {
52 attacker.message("This player is already affected by this spell!");
53 return false;
54 }
55 } else if (defender.isNpc()) {
56 attacker.message("You can't teleblock a npc!");
57 return false;
58 }
59 }
60
62 if (defender.isPlayer()) {
63 attacker.message("You can not use the crumble undead spell on a player!");
64 return false;
65 }
66
67 if (defender.isNpc()) {
68 String name = defender.getName().toLowerCase();
69
70 if (!(name.contains("zombified") || name.contains("undead") || name.contains("zombie") || name.contains("skeleton") || name.contains("ghost") || name.contains("shade"))) {
71 attacker.message("This spell only affects skeletons, zombies, ghosts and shades");
72 return false;
73 }
74 }
75 }
76
77 if (/*PlayerRight.isDeveloper(attacker) ||*/ spell.canCast(attacker, defender)) {
78 return true;
79 }
80
81 attacker.send(new SendMessage("You need some runes to cast this spell."));
82 attacker.getCombat().reset();
83 return false;
84 }
85
86 @Override
87 public void start(Player attacker, Mob defender, Hit[] hits) {
88 if (attacker.getCombat().getDefender() == defender) {
89 attacker.animate(getAttackAnimation(attacker, defender), true);
90 spell.getStart().ifPresent(attacker::graphic);
91
92 int projectileDuration = spell.sendProjectile(attacker, defender);
93 if (projectileDuration == 0) {
94 final int distance = attacker.getPosition().getChebyshevDistance(defender.getPosition());
95 final int duration = distance * 10;
96 projectileDuration = 51 + duration;
97 }
98 // so this is special just for ice barrage/burst, the projectile does this.
99 final Graphic endGraphic = getEndGraphic(spell.getEnd(), missed(hits), SPLASH, projectileDuration);
100 if (endGraphic != null) {
101 defender.graphic(endGraphic);
102 }
103
104 if (spell.getEffect().isPresent()) {
105 List<Hit> extra = new LinkedList<>();
106 for (Hit hit : hits) {
107 Predicate<CombatImpact> filter = effect -> effect.canAffect(attacker, defender, hit);
108 Consumer<CombatImpact> execute = effect -> effect.impact(attacker, defender, hit, extra);
109 spell.getEffect().filter(filter).ifPresent(execute);
110 }
111
112 if (!defender.isPlayer() || !PlayerRight.isIronman(attacker)) {
113 if (extra.isEmpty()) {
114 Collections.addAll(extra, hits);
115 addCombatExperience(attacker, spell.getBaseExperience(), extra.toArray(new Hit[0]));
116 } else {
117 addCombatExperience(attacker, spell.getBaseExperience(), hits);
118 }
119 } else {
120 attacker.skills.addExperience(Skill.MAGIC, spell.getBaseExperience());
121 }
122 } else if (!defender.isPlayer() || !PlayerRight.isIronman(attacker)) {
123 addCombatExperience(attacker, spell.getBaseExperience(), hits);
124 } else {
125 attacker.skills.addExperience(Skill.MAGIC, spell.getBaseExperience());
126 }
127
128 if ((!attacker.equipment.containsAny(11791, 12904) || !RandomUtils.success(0.125))) {
129 MagicRune.remove(attacker, spell.getRunes());
130 }
131
132 if (attacker.isSingleCast()) {
133 attacker.setSingleCast(null);
134 attacker.getCombat().reset();
135 attacker.interact(defender);
136 }
137
139 if(weapon != null) {
140 switch(weapon.getId()) {
142 attacker.thammoranSceptreCharges--;
143 }
144 }
145 }
146 }
147 }
148
149 @Override
150 public void hit(Player attacker, Mob defender, Hit hit) {
151 if (!attacker.isSingleCast() && !attacker.isAutocast()) {
152 attacker.resetFace();
153 }
154
155 if (hit.isAccurate()) {
156 if (attacker.equipment.retrieve(WEAPON_SLOT).filter(item -> item.getId() == 12904).isPresent() && RandomUtils.success(0.25)) {
157 defender.venom();
158 }
159 }
160 }
161
162 @Override
163 public void hitsplat(Player attacker, Mob defender, Hit hit) {
164 }
165
166 @Override
167 public CombatHit[] getHits(Player attacker, Mob defender) {
168 CombatHit combatHit = nextMagicHit(attacker, defender, spell.getCombatProjectile());
169
170 if (spell == CombatSpell.CRUMBLE_UNDEAD && defender.getName().equalsIgnoreCase("zombified spawn")) {
171 combatHit.setDamage(defender.getCurrentHealth());
172 }
173
174 return new CombatHit[]{combatHit};
175 }
176
177 @Override
178 public int getAttackDelay(Player attacker, Mob defender, FightType fightType) {
179 return 5;
180 }
181
182 @Override
183 public int getAttackDistance(Player attacker, FightType fightType) {
184 return 10;
185 }
186
187 @Override
188 public Animation getAttackAnimation(Player attacker, Mob defender) {
189 if (spell.getAnimation().isPresent()) {
190 return spell.getAnimation().get();
191 }
192
193 FightType fightType = attacker.getCombat().getFightType();
194 int animation = fightType.getAnimation();
195
196 if (attacker.equipment.hasShield()) {
197 Item weapon = attacker.equipment.getShield();
198 animation = weapon.getAttackAnimation(fightType).orElse(animation);
199 }
200
201 if (attacker.equipment.hasWeapon()) {
202 Item weapon = attacker.equipment.getWeapon();
203 animation = weapon.getAttackAnimation(fightType).orElse(animation);
204 }
205
206 return new Animation(animation, UpdatePriority.HIGH);
207 }
208
209 @Override
211 return CombatType.MAGIC;
212 }
213
215 return spell;
216 }
217}
Class that models a single animation used by an entity.
Represents a single graphic that can be used by entities.
Definition Graphic.java:10
abstract String getName()
Gets the name of this entity.
A wrapper for a Hit object, adding additional variables for hit and hitsplat delays.
A Hit object holds the damage amount and hitsplat data.
Definition Hit.java:10
void setDamage(int damage)
Sets the hit damage.
Definition Hit.java:98
final CombatHit nextMagicHit(T attacker, Mob defender)
static Graphic getEndGraphic(final CombatProjectile combatProjectile, final boolean splash, final Graphic splashGraphic)
static final Graphic SPLASH
The spell splash graphic.
static void addCombatExperience(Player player, double base, Hit... hits)
PlayerMagicStrategy(CombatSpell spell)
Constructs a new SpellStrategy from a CombatSpell.
int getAttackDelay(Player attacker, Mob defender, FightType fightType)
Handles the mob class.
Definition Mob.java:66
void interact(Mob mob)
Sets the mob interacting with another mob.
Definition Mob.java:278
final boolean isNpc()
Check if an entity is an npc.
Definition Mob.java:550
final boolean isPlayer()
Check if an entity is a player.
Definition Mob.java:564
void resetFace()
Resets the mob's face location.
Definition Mob.java:330
Optional< Graphic > graphic
Definition Mob.java:91
void venom()
Applies venom to the entity.
Definition Mob.java:508
This class represents a character controlled by a player.
Definition Player.java:125
void setSingleCast(CombatSpell singleCast)
Definition Player.java:848
Combat< Player > getCombat()
The combat of the mob.
Definition Player.java:759
Represents a trainable and usable skill.
Definition Skill.java:18
static final int MAGIC
The magic skill id.
Definition Skill.java:39
void addExperience(int id, double experience)
Adds experience to a given skill.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final Item get(int index)
Gets the Item located on index.
final Optional< Item > retrieve(int index)
Retrieves the item located on index.
final boolean containsAny(int... identifiers)
Determines if this container contains any identifiers.
The container that manages the equipment for a player.
The OutgoingPacket that sends a message to a Players chatbox in the client.
A static-util class that provides additional functionality for generating pseudo-random numbers.
static boolean success(double value)
Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to va...
Represents different priorities for updating.
The enumerated type whose elements represent the fighting types.
final int getAnimation()
Gets the animation executed when this type is active.
static void remove(Player player, Item[] required)
static boolean isIronman(Player player)
Checks if the player is an ironman.