RuneHive-Game
Loading...
Searching...
No Matches
CombatDamage.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat;
2
3import com.runehive.game.world.entity.combat.hit.Hit;
4import com.runehive.game.world.entity.mob.Mob;
5import com.runehive.game.world.entity.mob.npc.Npc;
6import com.runehive.game.world.entity.mob.player.Player;
7import com.runehive.game.world.entity.mob.player.PlayerRight;
8import com.runehive.util.Stopwatch;
9
10import java.util.HashMap;
11import java.util.Map;
12import java.util.Optional;
13import java.util.concurrent.TimeUnit;
14
15/**
16 * A fs of players who have inflicted damage on another player in a combat
17 * session.
18 *
19 * @author lare96 <http://github.com/lare96>
20 */
21public final class CombatDamage {
22
23 /**
24 * The damages of players who have inflicted damage.
25 */
26 private final Map<Mob, DamageCounter> attackers = new HashMap<>();
27
28 public Hit lastHit;
29
30 /**
31 * Registers damage in the backing collection for {@code character}. This
32 * method has no effect if the character isn't a {@code PLAYER} or if
33 * {@code amount} is below {@code 0}.
34 *
35 * @param character the character to register damage for.
36 * @param hit the hit to register.
37 */
38 public void add(Mob character, Hit hit) {
39 if (hit.getDamage() > 0) {
40 DamageCounter counter = attackers.putIfAbsent(character, new DamageCounter(hit.getDamage()));
41 if (counter != null)
42 counter.incrementAmount(hit.getDamage());
43 lastHit = hit;
44 }
45 }
46
47 /**
48 * Determines which player in the backing collection has inflicted the most
49 * damage.
50 *
51 * @return the player who has inflicted the most damage, or an empty
52 * optional if there are no entries.
53 */
54 public Optional<Npc> getNpcKiller() {
55 int amount = 0;
56 Npc killer = null;
57 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
58 DamageCounter counter = entry.getValue();
59 Mob entity = entry.getKey();
60
61 if (!entity.isNpc() || entity.isDead() || !entity.isValid() || counter.isTimeout())
62 continue;
63 if (counter.getAmount() > amount) {
64 amount = counter.getAmount();
65 killer = entity.getNpc();
66 }
67 }
68 return Optional.ofNullable(killer);
69 }
70
71 /**
72 * Determines which player in the backing collection has inflicted the most
73 * damage.
74 *
75 * @return the player who has inflicted the most damage, or an empty
76 * optional if there are no entries.
77 */
78 public Optional<Player> getPlayerKiller() {
79 int amount = 0;
80 Player killer = null;
81 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
82 DamageCounter counter = entry.getValue();
83 Mob entity = entry.getKey();
84
85 if (!entity.isPlayer() || entity.isDead() || entity.isValid() || counter.isTimeout())
86 continue;
87 if (counter.getAmount() > amount) {
88 amount = counter.getAmount();
89 killer = entity.getPlayer();
90 }
91 }
92 return Optional.ofNullable(killer);
93 }
94
95 /**
96 * Determines which entity in the backing collection has inflicted the most
97 * damage.
98 *
99 * @return the player who has inflicted the most damage, or an empty
100 * optional if there are no entries.
101 */
102 public Optional<Mob> calculateProperKiller() {
103 int amount = 0;
104 Mob killer = null;
105 for (Map.Entry<Mob, DamageCounter> entry : attackers.entrySet()) {
106 DamageCounter counter = entry.getValue();
107 Mob mob = entry.getKey();
108
109 if (mob.isDead() || !mob.isValid() || counter.isTimeout())
110 continue;
111
112 if (attackers.size() > 1 && mob.isPlayer() && PlayerRight.isIronman(mob.getPlayer()))
113 continue;
114
115 if (counter.getAmount() > amount) {
116 amount = counter.getAmount();
117 killer = mob;
118 }
119 }
120 return Optional.ofNullable(killer);
121 }
122
123 /**
124 * Clears all data from the backing collection.
125 */
126 public void clear() {
127 attackers.clear();
128 }
129
130 /**
131 * A counter that will track the amount of damage dealt and whether that
132 * damaged has timed out or not.
133 *
134 * @author lare96 <http://github.com/lare96>
135 */
136 private static final class DamageCounter {
137
138 /**
139 * The amount of damage within this counter.
140 */
141 private int amount;
142
143 /**
144 * The stopwatch that will determine when a timeout occurs.
145 */
147
148 /**
149 * Creates a new {@link DamageCounter}.
150 *
151 * @param amount the amount of damage within this counter.
152 */
153 public DamageCounter(int amount) {
154 this.amount = amount;
155 }
156
157 /**
158 * Gets the amount of damage within this counter.
159 *
160 * @return the amount of damage.
161 */
162 public int getAmount() {
163 return amount;
164 }
165
166 /**
167 * Increments the amount of damage within this counter.
168 *
169 * @param amount the amount to increment by.
170 */
171 public void incrementAmount(int amount) {
172 if (this.isTimeout()) {
173 this.amount = 0;
174 }
175 this.amount += amount;
176 this.stopwatch.reset();
177 }
178
179 /**
180 * Determines if this counter has timed out or not.
181 *
182 * @return {@code true} if this counter has timed out, {@code false}
183 * otherwise.
184 */
185 public boolean isTimeout() {
186 return stopwatch.elapsed(CombatConstants.DAMAGE_CACHE_TIMEOUT, TimeUnit.SECONDS);
187 }
188 }
189}
static final long DAMAGE_CACHE_TIMEOUT
The amount of time it takes in seconds for cached damage to timeout.
A counter that will track the amount of damage dealt and whether that damaged has timed out or not.
void incrementAmount(int amount)
Increments the amount of damage within this counter.
boolean isTimeout()
Determines if this counter has timed out or not.
final Stopwatch stopwatch
The stopwatch that will determine when a timeout occurs.
int amount
The amount of damage within this counter.
int getAmount()
Gets the amount of damage within this counter.
A fs of players who have inflicted damage on another player in a combat session.
Optional< Mob > calculateProperKiller()
Determines which entity in the backing collection has inflicted the most damage.
Optional< Player > getPlayerKiller()
Determines which player in the backing collection has inflicted the most damage.
Optional< Npc > getNpcKiller()
Determines which player in the backing collection has inflicted the most damage.
void add(Mob character, Hit hit)
Registers damage in the backing collection for character.
final Map< Mob, DamageCounter > attackers
The damages of players who have inflicted damage.
void clear()
Clears all data from the backing collection.
A Hit object holds the damage amount and hitsplat data.
Definition Hit.java:10
Handles the mob class.
Definition Mob.java:66
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
static Stopwatch start()
static boolean isIronman(Player player)
Checks if the player is an ironman.