RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Npc.java
1package com.osroyale.game.world.entity.mob.npc;
2
3import com.osroyale.content.activity.Activity;
4import com.osroyale.game.world.World;
5import com.osroyale.game.world.entity.EntityType;
6import com.osroyale.game.world.entity.combat.Combat;
7import com.osroyale.game.world.entity.combat.strategy.CombatStrategy;
8import com.osroyale.game.world.entity.mob.Direction;
9import com.osroyale.game.world.entity.mob.Mob;
10import com.osroyale.game.world.entity.mob.data.PacketType;
11import com.osroyale.game.world.entity.mob.npc.definition.NpcDefinition;
12import com.osroyale.game.world.position.Area;
13import com.osroyale.game.world.position.Position;
14import com.osroyale.game.world.region.Region;
15import com.osroyale.util.RandomUtils;
16import com.osroyale.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
65
66public class Npc extends Mob {
67 private static final Logger logger = LogManager.getLogger(Npc.class);
68 private int sequence;
69 public boolean walk;
70 public boolean canAttack;
71 public Mob owner;
72 public final Direction faceDirection;
73 public final Position spawnPosition;
74 public NpcDefinition definition;
75 private CombatStrategy<Npc> strategy;
76 public final NpcAssistant npcAssistant = new NpcAssistant(this);
77 private final Combat<Npc> combat = new Combat<>(this);
78 private int walkCooldown;
79 public Position[] boundaries;
80 public int walkingRadius;
81 public final AtomicInteger atomicPlayerCount = new AtomicInteger(0);
82
83 public Npc(int id, Position position) {
84 this(id, position, 0, Mob.DEFAULT_INSTANCE, Direction.SOUTH);
85 }
86
87 public Npc(int id, int instance, Position position) {
88 super(position);
89 this.id = id;
90 this.instance = instance;
91 this.faceDirection = Direction.SOUTH;
92 this.spawnPosition = position;
93 this.walkingRadius = 0;
94 }
95
96 public Npc(int id, Position position, int walkingRadius, int instance, Direction direction) {
97 super(position);
98 this.id = id;
99 this.instance = instance;
100 this.faceDirection = direction;
101 this.spawnPosition = position.copy();
102 this.walkingRadius = walkingRadius;
103 }
104
105 public Npc(int id, Position position, int walkingRadius, Direction direction) {
106 super(position);
107 this.id = id;
108 this.faceDirection = direction;
109 this.spawnPosition = position.copy();
110 this.walkingRadius = walkingRadius;
111 }
112
113
114 @Override
115 public void sequence() {
116 try {
117 action.sequence();
118
119 if (sequence % 110 == 0) {
120 for (int index = 0; index <= 6; index++) {
121 skills.regress(index);
122 }
123 }
124
125 if (definition.isAttackable() && (definition.isAggressive() || Area.inWilderness(this))) {
126 getCombat().checkAggression(spawnPosition);
127 }
128
129 if (!isDead() && !locking.locked(PacketType.MOVEMENT) && combat.getDefender() == null && !combat.inCombat() && walk) {
130 if (walkCooldown > 0) {
131 walkCooldown--;
132 } else {
133 Position target = Utility.randomElement(boundaries);
134 walk(target);
135 walkCooldown = RandomUtils.inclusive(10, 30);
136 }
137 }
138
139 if (definition.isAttackable()) {
140 getCombat().tick();
141 }
142 } catch (Exception ex) {
143 logger.error(String.format("error npc.sequence(): %s", this), ex);
144 }
145 sequence++;
146 }
147
148 @Override
149 public void register() {
150 if (!isRegistered() && !World.getNpcs().contains(this)) {
151 int w = walkingRadius * width();
152 int l = walkingRadius * length();
153
154 walk = walkingRadius != 0;
155 boundaries = Utility.getInnerBoundaries(spawnPosition.transform(-w, -l), w * 2, l * 2);
156 setRegistered(World.getNpcs().add(this));
157 setPosition(getPosition());
158 npcAssistant.login();
159 }
160 }
161
162 @Override
163 public void unregister() {
164 if (isRegistered()) {
165 World.getNpcs().remove((Npc) destroy());
166 }
167 }
168
169 @Override
170 public void addToRegion(Region region) {
171 region.addNpc(this);
172 }
173
174 @Override
175 public void removeFromRegion(Region region) {
176 region.removeNpc(this);
177 }
178
179 @Override
180 public void onStep() {
181 }
182
183 @Override
184 public void appendDeath() {
185 if (inActivity()) {
186 Activity.forActivity(this, activity -> activity.onDeath(this));
187 return;
188 }
189 World.schedule(new NpcDeath(this));
190 }
191
192 @Override
193 public Combat<Npc> getCombat() {
194 return combat;
195 }
196
197 @Override
198 public CombatStrategy<Npc> getStrategy() {
199 return strategy;
200 }
201
202 @Override
203 public String getName() {
204 return definition == null ? "Unknown" : definition.getName();
205 }
206
207 @Override
208 public int getBonus(int index) {
209 return definition.getBonuses()[index];
210 }
211
212 @Override
213 public int[] getBonuses() {
214 return definition.getBonuses();
215 }
216
217 @Override
219 return EntityType.NPC;
220 }
221
222 @Override
223 public int hashCode() {
224 return Objects.hash(getIndex(), definition);
225 }
226
227 @Override
228 public boolean equals(Object obj) {
229 if (obj instanceof Npc) {
230 Npc other = (Npc) obj;
231 return id == other.id && getIndex() == other.getIndex() && spawnPosition.equals(other.spawnPosition);
232 }
233 return obj == this;
234 }
235
236 @Override
237 public String toString() {
238 return String.format("Npc[name=" + getName() + " index=%d id=%d registered=%s instance=%s %s]", getIndex(), id, isRegistered(), instance, getPosition());
239 }
240
241 int getDeathTime() {
242 return definition.getDeathTimer();
243 }
244
245 public boolean isAutoRetaliate() {
246 return definition.isRetaliate();
247 }
248
249 public void setStrategy(CombatStrategy<Npc> strategy) {
250 this.strategy = strategy;
251 }
252
253 @Override
254 public int getPriorityIndex() {
255 return -getListIndex();
256 }
257
261 public int pyroHealth = 24;
262 public boolean pyroSnowAttack;
263}
static void schedule(Task task)
Definition World.java:284
CombatStrategy< Npc > getStrategy()
Definition Npc.java:198
static< T > T randomElement(Collection< T > collection)
Definition Utility.java:285