RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
NpcMagicStrategy.java
1package com.osroyale.game.world.entity.combat.strategy.npc;
2
3import com.osroyale.game.Animation;
4import com.osroyale.game.Graphic;
5import com.osroyale.game.UpdatePriority;
6import com.osroyale.game.engine.GameEngine;
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.effect.impl.CombatPoisonEffect;
11import com.osroyale.game.world.entity.combat.effect.impl.CombatVenomEffect;
12import com.osroyale.game.world.entity.combat.hit.CombatHit;
13import com.osroyale.game.world.entity.combat.hit.Hit;
14import com.osroyale.game.world.entity.combat.projectile.CombatProjectile;
15import com.osroyale.game.world.entity.combat.strategy.basic.MagicStrategy;
16import com.osroyale.game.world.entity.mob.Mob;
17import com.osroyale.game.world.entity.mob.npc.Npc;
18import com.osroyale.util.RandomUtils;
19import org.jire.tarnishps.WorldTask;
20
21import javax.annotation.Nullable;
22import java.util.Optional;
23import java.util.function.Consumer;
24import java.util.function.Predicate;
25
26public class NpcMagicStrategy extends MagicStrategy<Npc> {
27
28 protected final CombatProjectile combatProjectile;
29
30 public NpcMagicStrategy(CombatProjectile combatProjectile) {
31 this.combatProjectile = combatProjectile;
32 }
33
34 @Override
35 public CombatProjectile getCombatProjectile() {
36 return combatProjectile;
37 }
38
39 @Override
40 public void start(Npc attacker, Mob defender, Hit[] hits) {
41 sendAnimation(attacker, defender);
42 sendProjectile(attacker, hits, attacker, defender, null);
43 }
44
45 public void sendAnimation(Npc attacker, Mob defender) {
46 Animation animation = getAttackAnimation(attacker, defender);
47 if (animation.isReset()) {
48 Optional<Animation> projAnim = combatProjectile.getAnimation();
49 if (projAnim.isPresent()) animation = projAnim.get();
50 }
51
52 attacker.animate(animation, true);
53 combatProjectile.getStart().ifPresent(attacker::graphic);
54 }
55
56 public int sendProjectile(Npc attacker, Hit[] hits,
57 Mob from, Mob to,
58 @Nullable Runnable onProjectileLand) {
59 final int duration = combatProjectile.sendProjectile(from, to);
60
61 final Graphic endGraphic = getEndGraphic(combatProjectile, missed(hits), SPLASH, duration);
62 if (endGraphic != null) to.graphic(endGraphic);
63
64 for (Hit hit : hits) {
65 Predicate<CombatImpact> filter = effect -> effect.canAffect(attacker, to, hit);
66 Consumer<CombatImpact> execute = effect -> effect.impact(attacker, to, hit, null);
67 combatProjectile.getEffect().filter(filter).ifPresent(execute);
68
69 if (attacker.definition.isPoisonous()) {
70 if (CombatVenomEffect.isVenomous(attacker) && RandomUtils.success(0.25)) {
71 to.venom();
72 } else {
73 CombatPoisonEffect.getPoisonType(attacker.id).ifPresent(to::poison);
74 }
75 }
76 }
77
78 if (onProjectileLand != null) {
79 final int delay = GameEngine.clientTicksToServerTicks(duration);
80 WorldTask.schedule(delay, onProjectileLand);
81 }
82
83 return duration;
84 }
85
86 @Override
87 public CombatHit[] getHits(Npc attacker, Mob defender) {
88 return new CombatHit[]{nextMagicHit(attacker, defender, combatProjectile)};
89 }
90
91 @Override
92 public int getAttackDelay(Npc attacker, Mob defender, FightType fightType) {
93 int delay = attacker.definition.getAttackDelay();
94
95 if (attacker.getPosition().getDistance(defender.getPosition()) > 4) {
96 return 1 + delay;
97 }
98
99 return delay;
100 }
101
102 @Override
103 public int getAttackDistance(Npc attacker, FightType fightType) {
104 return 10;
105 }
106
107 @Override
108 public Animation getAttackAnimation(Npc attacker, Mob defender) {
109 return new Animation(attacker.definition.getAttackAnimation(), UpdatePriority.HIGH);
110 }
111
112 @Override
113 public boolean canAttack(Npc attacker, Mob defender) {
114 return true;
115 }
116
117 @Override
118 public CombatType getCombatType() {
119 return CombatType.MAGIC;
120 }
121
122}