RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
PlayerMagicStrategy.java
1package com.osroyale.game.world.entity.combat.strategy.player;
2
3import com.osroyale.content.itemaction.impl.ThammaronsSceptre;
4import com.osroyale.game.Animation;
5import com.osroyale.game.Graphic;
6import com.osroyale.game.UpdatePriority;
7import com.osroyale.game.world.entity.combat.CombatImpact;
8import com.osroyale.game.world.entity.combat.CombatType;
9import com.osroyale.game.world.entity.combat.attack.FightType;
10import com.osroyale.game.world.entity.combat.hit.CombatHit;
11import com.osroyale.game.world.entity.combat.hit.Hit;
12import com.osroyale.game.world.entity.combat.magic.CombatSpell;
13import com.osroyale.game.world.entity.combat.magic.MagicRune;
14import com.osroyale.game.world.entity.combat.strategy.basic.MagicStrategy;
15import com.osroyale.game.world.entity.mob.Mob;
16import com.osroyale.game.world.entity.mob.player.Player;
17import com.osroyale.game.world.entity.mob.player.PlayerRight;
18import com.osroyale.game.world.entity.skill.Skill;
19import com.osroyale.game.world.items.Item;
20import com.osroyale.game.world.items.containers.equipment.Equipment;
21import com.osroyale.net.packet.out.SendMessage;
22import com.osroyale.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.osroyale.game.world.items.containers.equipment.Equipment.WEAPON_SLOT;
31
32public class PlayerMagicStrategy extends MagicStrategy<Player> {
33
35 private final CombatSpell spell;
36
38 public PlayerMagicStrategy(CombatSpell spell) {
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
49 if (spell == CombatSpell.TELE_BLOCK) {
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
61 if (spell == CombatSpell.CRUMBLE_UNDEAD) {
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
138 Item weapon = attacker.equipment.get(Equipment.WEAPON_SLOT);
139 if(weapon != null) {
140 switch(weapon.getId()) {
141 case ThammaronsSceptre.THAMMARONS_SCEPTRE_CHARGED_ID -> {
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
210 public CombatType getCombatType() {
211 return CombatType.MAGIC;
212 }
213
214 public CombatSpell getSpell() {
215 return spell;
216 }
217}