RuneHive-Game
Loading...
Searching...
No Matches
Npc.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.npc;
2
3import com.runehive.content.activity.Activity;
4import com.runehive.game.world.World;
5import com.runehive.game.world.entity.EntityType;
6import com.runehive.game.world.entity.combat.Combat;
7import com.runehive.game.world.entity.combat.strategy.CombatStrategy;
8import com.runehive.game.world.entity.mob.Direction;
9import com.runehive.game.world.entity.mob.Mob;
10import com.runehive.game.world.entity.mob.data.PacketType;
11import com.runehive.game.world.entity.mob.npc.definition.NpcDefinition;
12import com.runehive.game.world.position.Area;
13import com.runehive.game.world.position.Position;
14import com.runehive.game.world.region.Region;
15import com.runehive.util.RandomUtils;
16import com.runehive.util.Utility;
17import org.apache.logging.log4j.LogManager;
18import org.apache.logging.log4j.Logger;
19
20import java.util.Objects;
21import java.util.concurrent.atomic.AtomicInteger;
22
23/**
24 * Represents a non-player character in the in-game world.
25 *
26 * @author Daniel | Obey
27 * @author Michael | Chex
28 */
29public class Npc extends Mob {
30 private static final Logger logger = LogManager.getLogger(Npc.class);
31 private int sequence;
32 public boolean walk;
33 public boolean canAttack;
34 public Mob owner;
36 public final Position spawnPosition;
39 public final NpcAssistant npcAssistant = new NpcAssistant(this);
40 private final Combat<Npc> combat = new Combat<>(this);
41 private int walkCooldown;
43 public int walkingRadius;
44 public final AtomicInteger atomicPlayerCount = new AtomicInteger(0);
45
46 public Npc(int id, Position position) {
47 this(id, position, 0, Mob.DEFAULT_INSTANCE, Direction.SOUTH);
48 }
49
50 public Npc(int id, int instance, Position position) {
51 super(position);
52 this.id = id;
53 this.instance = instance;
54 this.faceDirection = Direction.SOUTH;
55 this.spawnPosition = position;
56 this.walkingRadius = 0;
57 }
58
59 public Npc(int id, Position position, int walkingRadius, int instance, Direction direction) {
60 super(position);
61 this.id = id;
62 this.instance = instance;
63 this.faceDirection = direction;
64 this.spawnPosition = position.copy();
65 this.walkingRadius = walkingRadius;
66 }
67
68 public Npc(int id, Position position, int walkingRadius, Direction direction) {
69 super(position);
70 this.id = id;
71 this.faceDirection = direction;
72 this.spawnPosition = position.copy();
73 this.walkingRadius = walkingRadius;
74 }
75
76
77 @Override
78 public void sequence() {
79 try {
80 action.sequence();
81
82 if (sequence % 110 == 0) {
83 for (int index = 0; index <= 6; index++) {
84 skills.regress(index);
85 }
86 }
87
88 if (definition.isAttackable() && (definition.isAggressive() || Area.inWilderness(this))) {
89 getCombat().checkAggression(spawnPosition);
90 }
91
92 if (!isDead() && !locking.locked(PacketType.MOVEMENT) && combat.getDefender() == null && !combat.inCombat() && walk) {
93 if (walkCooldown > 0) {
95 } else {
97 walk(target);
99 }
100 }
101
102 if (definition.isAttackable()) {
103 getCombat().tick();
104 }
105 } catch (Exception ex) {
106 logger.error(String.format("error npc.sequence(): %s", this), ex);
107 }
108 sequence++;
109 }
110
111 @Override
112 public void register() {
113 if (!isRegistered() && !World.getNpcs().contains(this)) {
114 int w = walkingRadius * width();
115 int l = walkingRadius * length();
116
117 walk = walkingRadius != 0;
118 boundaries = Utility.getInnerBoundaries(spawnPosition.transform(-w, -l), w * 2, l * 2);
119 setRegistered(World.getNpcs().add(this));
121 npcAssistant.login();
122 }
123 }
124
125 @Override
126 public void unregister() {
127 if (isRegistered()) {
128 World.getNpcs().remove((Npc) destroy());
129 }
130 }
131
132 @Override
133 public void addToRegion(Region region) {
134 region.addNpc(this);
135 }
136
137 @Override
139 region.removeNpc(this);
140 }
141
142 @Override
143 public void onStep() {
144 }
145
146 @Override
147 public void appendDeath() {
148 if (inActivity()) {
149 Activity.forActivity(this, activity -> activity.onDeath(this));
150 return;
151 }
152 World.schedule(new NpcDeath(this));
153 }
154
155 @Override
157 return combat;
158 }
159
160 @Override
162 return strategy;
163 }
164
165 @Override
166 public String getName() {
167 return definition == null ? "Unknown" : definition.getName();
168 }
169
170 @Override
171 public int getBonus(int index) {
172 return definition.getBonuses()[index];
173 }
174
175 @Override
176 public int[] getBonuses() {
177 return definition.getBonuses();
178 }
179
180 @Override
182 return EntityType.NPC;
183 }
184
185 @Override
186 public int hashCode() {
187 return Objects.hash(getIndex(), definition);
188 }
189
190 @Override
191 public boolean equals(Object obj) {
192 if (obj instanceof Npc) {
193 Npc other = (Npc) obj;
194 return id == other.id && getIndex() == other.getIndex() && spawnPosition.equals(other.spawnPosition);
195 }
196 return obj == this;
197 }
198
199 @Override
200 public String toString() {
201 return String.format("Npc[name=" + getName() + " index=%d id=%d registered=%s instance=%s %s]", getIndex(), id, isRegistered(), instance, getPosition());
202 }
203
205 return definition.getDeathTimer();
206 }
207
208 public boolean isAutoRetaliate() {
209 return definition.isRetaliate();
210 }
211
213 this.strategy = strategy;
214 }
215
216 @Override
217 public int getPriorityIndex() {
218 return -getListIndex();
219 }
220
221 /**
222 * Pyromancer data
223 */
224 public int pyroHealth = 24;
225 public boolean pyroSnowAttack;
226}
A Activity object constructs an in-game activity and sequences it through the start() and finish() me...
Definition Activity.java:31
static void forActivity(Mob mob, Consumer< Activity > consumer)
Definition Activity.java:78
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
static MobList< Npc > getNpcs()
Definition World.java:548
void setRegistered(boolean registered)
Definition Entity.java:98
Entity destroy()
Destroys this entity.
Definition Entity.java:143
void setPosition(Position position)
Definition Entity.java:106
Mob(Position position)
Constructs a new Mob.
Definition Mob.java:114
Method handles small methods for npcs that do not have any parent class.
boolean isAutoRetaliate()
State of the mob's auto retaliate.
Definition Npc.java:208
String getName()
Gets the name of this entity.
Definition Npc.java:166
void addToRegion(Region region)
Adds this entity to the specified region.
Definition Npc.java:133
void appendDeath()
Handles the mob death.
Definition Npc.java:147
void sequence()
The method which is invoked every tick.
Definition Npc.java:78
final AtomicInteger atomicPlayerCount
Definition Npc.java:44
EntityType getType()
Gets the EntityType.
Definition Npc.java:181
Npc(int id, Position position, int walkingRadius, int instance, Direction direction)
Definition Npc.java:59
void removeFromRegion(Region region)
Removes this entity from the specified region.
Definition Npc.java:138
void setStrategy(CombatStrategy< Npc > strategy)
Definition Npc.java:212
Npc(int id, Position position)
Definition Npc.java:46
void register()
Registers an entity to the World.
Definition Npc.java:112
Npc(int id, int instance, Position position)
Definition Npc.java:50
CombatStrategy< Npc > getStrategy()
The combat strategy of the mob.
Definition Npc.java:161
void unregister()
Unregisters an entity from the World.
Definition Npc.java:126
Npc(int id, Position position, int walkingRadius, Direction direction)
Definition Npc.java:68
Combat< Npc > getCombat()
The combat of the mob.
Definition Npc.java:156
Handles checking if mobs are in a certain area.
Definition Area.java:13
static boolean inWilderness(Position position)
Definition Area.java:272
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
A static-util class that provides additional functionality for generating pseudo-random numbers.
static int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.
Handles miscellaneous methods.
Definition Utility.java:27
static< T > T randomElement(Collection< T > collection)
Picks a random element out of any array type.
Definition Utility.java:248
static Position[] getInnerBoundaries(Position position, int width, int length)
Definition Utility.java:621
Represents the enumerated directions an entity can walk or face.