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