RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CombatDamage.java
1package com.osroyale.game.world.entity.combat;
2
3import com.osroyale.game.world.entity.combat.hit.Hit;
4import com.osroyale.game.world.entity.mob.Mob;
5import com.osroyale.game.world.entity.mob.npc.Npc;
6import com.osroyale.game.world.entity.mob.player.Player;
7import com.osroyale.game.world.entity.mob.player.PlayerRight;
8import com.osroyale.util.Stopwatch;
9
10import java.util.HashMap;
11import java.util.Map;
12import java.util.Optional;
13import java.util.concurrent.TimeUnit;
14
57
58public final class CombatDamage {
59
63 private final Map<Mob, DamageCounter> attackers = new HashMap<>();
64
65 public Hit lastHit;
66
75 public void add(Mob character, Hit hit) {
76 if (hit.getDamage() > 0) {
77 DamageCounter counter = attackers.putIfAbsent(character, new DamageCounter(hit.getDamage()));
78 if (counter != null)
79 counter.incrementAmount(hit.getDamage());
80 lastHit = hit;
81 }
82 }
83
91 public Optional<Npc> getNpcKiller() {
92 int amount = 0;
93 Npc killer = null;
94 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
95 DamageCounter counter = entry.getValue();
96 Mob entity = entry.getKey();
97
98 if (!entity.isNpc() || entity.isDead() || !entity.isValid() || counter.isTimeout())
99 continue;
100 if (counter.getAmount() > amount) {
101 amount = counter.getAmount();
102 killer = entity.getNpc();
103 }
104 }
105 return Optional.ofNullable(killer);
106 }
107
115 public Optional<Player> getPlayerKiller() {
116 int amount = 0;
117 Player killer = null;
118 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
119 DamageCounter counter = entry.getValue();
120 Mob entity = entry.getKey();
121
122 if (!entity.isPlayer() || entity.isDead() || entity.isValid() || counter.isTimeout())
123 continue;
124 if (counter.getAmount() > amount) {
125 amount = counter.getAmount();
126 killer = entity.getPlayer();
127 }
128 }
129 return Optional.ofNullable(killer);
130 }
131
139 public Optional<Mob> calculateProperKiller() {
140 int amount = 0;
141 Mob killer = null;
142 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
143 DamageCounter counter = entry.getValue();
144 Mob mob = entry.getKey();
145
146 if (mob.isDead() || !mob.isValid() || counter.isTimeout())
147 continue;
148
149 if (attackers.size() > 1 && mob.isPlayer() && PlayerRight.isIronman(mob.getPlayer()))
150 continue;
151
152 if (counter.getAmount() > amount) {
153 amount = counter.getAmount();
154 killer = mob;
155 }
156 }
157 return Optional.ofNullable(killer);
158 }
159
163 public void clear() {
164 attackers.clear();
165 }
166
173 private static final class DamageCounter {
174
178 private int amount;
179
183 private final Stopwatch stopwatch = Stopwatch.start();
184
190 public DamageCounter(int amount) {
191 this.amount = amount;
192 }
193
199 public int getAmount() {
200 return amount;
201 }
202
208 public void incrementAmount(int amount) {
209 if (this.isTimeout()) {
210 this.amount = 0;
211 }
212 this.amount += amount;
213 this.stopwatch.reset();
214 }
215
222 public boolean isTimeout() {
223 return stopwatch.elapsed(CombatConstants.DAMAGE_CACHE_TIMEOUT, TimeUnit.SECONDS);
224 }
225 }
226}