RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
NpcDefinition.java
1package com.osroyale.game.world.entity.mob.npc.definition;
2
3import com.google.gson.JsonObject;
4import com.osroyale.Config;
5import com.osroyale.fs.cache.decoder.CacheNpcDefinition;
6import com.osroyale.game.world.entity.combat.CombatType;
7import com.osroyale.game.world.entity.combat.projectile.CombatProjectile;
8import com.osroyale.game.world.entity.mob.player.Player;
9import com.osroyale.util.parser.GsonParser;
10import org.jire.tarnishps.OldToNew;
11import org.jire.tarnishps.defs.MonsterDefLoader;
12
13import java.util.*;
14
15import static com.osroyale.game.world.entity.combat.CombatConstants.*;
16
58
59public class NpcDefinition {
60
62 public static final NpcDefinition[] DEFINITIONS = new NpcDefinition[Config.NPC_DEFINITION_LIMIT + 1];
63
64 private final int id;
65 private final String name;
66 private int combatLevel;
67 private int size;
68 private int stand;
69 private int walk;
70 private int turn180;
71 private int turn90CW;
72 private int turn90CCW;
73 private int attackAnim;
74 private int blockAnim;
75 private int deathAnim;
76 private int deathTimer;
77 private boolean attackable;
78 private boolean aggressive;
79 private boolean retaliate;
80 private boolean poisonous;
81 private boolean poisonImmunity;
82 private boolean venomImmunity;
83 private boolean flying;
84 private int[] skills;
85 private int[] bonuses;
86 private int attackDelay;
87 private int attackRadius;
88 private int respawnTime;
89 private CombatAttackData combatAttackData;
90 private Player player;
91
92 public NpcDefinition(int id, String name) {
93 this.id = id;
94 this.name = name;
95 this.combatLevel = 0;
96 this.size = 1;
97 this.stand = -1;
98 this.walk = -1;
99 this.turn180 = -1;
100 this.turn90CW = -1;
101 this.turn90CCW = -1;
102 this.attackAnim = -1;
103 this.blockAnim = -1;
104 this.deathAnim = -1;
105 this.deathTimer = 3;
106 this.flying = false;
107 this.attackable = false;
108 this.aggressive = false;
109 this.retaliate = false;
110 this.poisonous = false;
111 this.poisonImmunity = false;
112 this.venomImmunity = false;
113 this.skills = EMPTY_SKILLS;
114 this.bonuses = EMPTY_BONUSES;
115 this.attackDelay = 4;
116 this.attackRadius = 1;
117 this.respawnTime = 30;
118 }
119
120 public static void main(String[] args) {
121 GsonParser parser = createParser();
122 parser.deserialize();
123 }
124
125 public static GsonParser createParser() {
126 return new GsonParser("def/npc/npc_definitions", false) {
127
128 @Override
129 protected void parse(JsonObject data) {
130 int id = data.get("id").getAsInt();
131 boolean convertId = true;
132 if (data.has("convert-id")) {
133 convertId = data.get("convert-id").getAsBoolean();
134 }
135 if (convertId) {
136 int newId = OldToNew.get(id);
137 if (newId != -1) {
138 id = newId;
139 }
140 }
141
142 final CacheNpcDefinition cacheDef = CacheNpcDefinition.lookup(id);
143 final String cacheDefName = cacheDef.name;
144 final NpcDefinition definition = new NpcDefinition(id, cacheDefName == null ? "" : cacheDefName);
145 definition.size = cacheDef.size;
146 definition.combatLevel = cacheDef.combatLevel;
147 definition.stand = cacheDef.standingAnimation;
148 definition.walk = cacheDef.walkingAnimation;
149 definition.turn180 = cacheDef.halfTurnAnimation;
150 definition.turn90CW = cacheDef.quarterClockwiseTurnAnimation;
151 definition.turn90CCW = cacheDef.quarterAnticlockwiseTurnAnimation;
152
153 definition.deathTimer = 3;
154 if (data.has("death-timer")) {
155 definition.deathTimer = data.get("death-timer").getAsInt();
156 }
157
158 if (data.has("attack-animation")) {
159 definition.attackAnim = data.get("attack-animation").getAsInt();
160 }
161
162 if (data.has("block-animation")) {
163 definition.blockAnim = data.get("block-animation").getAsInt();
164 }
165
166 if (data.has("death-animation")) {
167 definition.deathAnim = data.get("death-animation").getAsInt();
168 }
169
170 if (data.has("attackable")) {
171 definition.attackable = data.get("attackable").getAsBoolean();
172 }
173
174 if (data.has("aggressive")) {
175 definition.aggressive = data.get("aggressive").getAsBoolean();
176 }
177
178 definition.retaliate = definition.isAttackable();
179 if (data.has("retaliate")) {
180 definition.retaliate = data.get("retaliate").getAsBoolean();
181 }
182
183 if (data.has("poisonous")) {
184 definition.poisonous = data.get("poisonous").getAsBoolean();
185 }
186
187 if (data.has("flying")) {
188 definition.flying = data.get("flying").getAsBoolean();
189 }
190
191 if (data.has("poison-immunity")) {
192 definition.poisonImmunity = data.get("poison-immunity").getAsBoolean();
193 }
194
195 if (data.has("venom-immunity")) {
196 definition.venomImmunity = data.get("venom-immunity").getAsBoolean();
197 }
198
199 if (data.has("attack-cooldown")) {
200 definition.attackDelay = data.get("attack-cooldown").getAsInt();
201 }
202
203 if (data.has("attack-radius")) {
204 definition.attackRadius = data.get("attack-radius").getAsInt();
205 }
206
207 if (data.has("combat-type") && data.has("projectile-key")) {
208 CombatType type = CombatType.valueOf(data.get("combat-type").getAsString());
209 String key = data.get("projectile-key").getAsString();
210 definition.combatAttackData = new CombatAttackData(type, key);
211 }
212
213 if (data.has("respawn")) {
214 definition.respawnTime = data.get("respawn").getAsInt();
215 }
216
217 for (int index = 0; index < SKILL_FIELD_NAMES.length; index++) {
218 String skillName = SKILL_FIELD_NAMES[index];
219 if (data.has(skillName)) {
220 if (definition.skills == EMPTY_SKILLS) {
221 definition.skills = new int[EMPTY_SKILLS.length];
222 }
223 definition.skills[index] = data.get(skillName).getAsInt();
224 }
225 }
226
227 for (int index = 0; index < BONUS_CONFIG_FIELD_NAMES.length; index++) {
228 String bonusName = BONUS_CONFIG_FIELD_NAMES[index];
229 if (data.has(bonusName)) {
230 if (definition.bonuses == EMPTY_BONUSES) {
231 definition.bonuses = new int[EMPTY_BONUSES.length];
232 }
233 definition.bonuses[index] = data.get(bonusName).getAsInt();
234 }
235 }
236
237 DEFINITIONS[id] = definition;
238 }
239
240 @Override
241 protected void onEnd() {
242 MonsterDefLoader.load();
243 }
244
245 };
246 }
247
249 public static NpcDefinition get(int id) {
252
253 if (id < 0)
254 id = 0;
255
256 if (DEFINITIONS[id] == null)
257 return new NpcDefinition(id, "null");
258
259 return DEFINITIONS[id];
260 }
261
262 public static NpcDefinition get(String name) {
263 for (NpcDefinition definition : DEFINITIONS) {
264 if (definition != null && definition.getName().equals(name)) {
265 return definition;
266 }
267 }
268 return null;
269 }
270
271 public static String getCombatLevelRange(String name) {
272 List<Integer> levels = new ArrayList<>();
273
274 for (NpcDefinition definition : DEFINITIONS) {
275 if (definition != null && definition.getName().contains(name)) {
276 levels.add(definition.getCombatLevel());
277 }
278 }
279
280 if (levels.isEmpty()) {
281 return "Unknown";
282 }
283
284 Collections.sort(levels);
285
286 return levels.get(0) + "-" + levels.get(levels.size() - 1);
287 }
288
290 public int getId() {
291 return id;
292 }
293
295 public String getName() {
296 return name;
297 }
298
300 public int getSize() {
301 return size;
302 }
303
305 public int getCombatLevel() {
306 return combatLevel;
307 }
308
310 public int getStand() {
311 return stand;
312 }
313
315 public int getWalk() {
316 return walk;
317 }
318
320 public int getTurn180() {
321 return turn180;
322 }
323
325 public int getTurn90CW() {
326 return turn90CW;
327 }
328
330 public int getTurn90CCW() {
331 return turn90CCW;
332 }
333
335 public int getAttackAnimation() {
336 return attackAnim;
337 }
338
340 public int getBlockAnimation() {
341 return blockAnim;
342 }
343
345 public int getDeathAnimation() {
346 return deathAnim;
347 }
348
349 public int getDeathTimer() {
350 return deathTimer;
351 }
352
354 public boolean isAttackable() {
355 return attackable;
356 }
357
359 public boolean isAggressive() {
360 return aggressive;
361 }
362
364 public boolean isRetaliate() {
365 return retaliate;
366 }
367
369 public boolean isPoisonous() {
370 return poisonous;
371 }
372
374 public boolean hasPoisonImmunity() {
375 return poisonImmunity;
376 }
377
379 public boolean hasVenomImmunity() {
380 return venomImmunity;
381 }
382
383 public boolean isFlying() {
384 return flying;
385 }
386
388 public int getAttackDelay() {
389 return attackDelay;
390 }
391
393 public int getAttackRadius() {
394 return attackRadius;
395 }
396
398 public int getRespawnTime() {
399 return respawnTime;
400 }
401
403 public int[] getSkills() {
404 return skills;
405 }
406
408 public int[] getBonuses() {
409 return bonuses;
410 }
411
412 public void setAggressive(boolean aggressive) {
413 this.aggressive = aggressive;
414 }
415
416 public void setRetaliate(boolean retaliate) {
417 this.retaliate = retaliate;
418 }
419
420 public void setRespawnTime(int respawnTime) {
421 this.respawnTime = respawnTime;
422 }
423
424 public void setCombatLevel(int combatLevel) {
425 this.combatLevel = combatLevel;
426 }
427
428 public void setSize(int size) {
429 this.size = size;
430 }
431
432 public void setStand(int stand) {
433 this.stand = stand;
434 }
435
436 public void setWalk(int walk) {
437 this.walk = walk;
438 }
439
440 public void setTurn180(int turn180) {
441 this.turn180 = turn180;
442 }
443
444 public void setTurn90CW(int turn90CW) {
445 this.turn90CW = turn90CW;
446 }
447
448 public void setTurn90CCW(int turn90CCW) {
449 this.turn90CCW = turn90CCW;
450 }
451
452 public int getAttackAnim() {
453 return attackAnim;
454 }
455
456 public void setAttackAnim(int attackAnim) {
457 this.attackAnim = attackAnim;
458 }
459
460 public int getBlockAnim() {
461 return blockAnim;
462 }
463
464 public void setBlockAnim(int blockAnim) {
465 this.blockAnim = blockAnim;
466 }
467
468 public int getDeathAnim() {
469 return deathAnim;
470 }
471
472 public void setDeathAnim(int deathAnim) {
473 this.deathAnim = deathAnim;
474 }
475
476 public void setDeathTimer(int deathTimer) {
477 this.deathTimer = deathTimer;
478 }
479
480 public void setAttackable(boolean attackable) {
481 this.attackable = attackable;
482 }
483
484 public void setPoisonous(boolean poisonous) {
485 this.poisonous = poisonous;
486 }
487
488 public boolean isPoisonImmunity() {
489 return poisonImmunity;
490 }
491
492 public void setPoisonImmunity(boolean poisonImmunity) {
493 this.poisonImmunity = poisonImmunity;
494 }
495
496 public boolean isVenomImmunity() {
497 return venomImmunity;
498 }
499
500 public void setVenomImmunity(boolean venomImmunity) {
501 this.venomImmunity = venomImmunity;
502 }
503
504 public void setFlying(boolean flying) {
505 this.flying = flying;
506 }
507
508 public void setSkills(int[] skills) {
509 this.skills = skills;
510 }
511
512 public void setBonuses(int[] bonuses) {
513 this.bonuses = bonuses;
514 }
515
516 public void setAttackDelay(int attackDelay) {
517 this.attackDelay = attackDelay;
518 }
519
520 public void setAttackRadius(int attackRadius) {
521 this.attackRadius = attackRadius;
522 }
523
524 public void setCombatAttackData(CombatAttackData combatAttackData) {
525 this.combatAttackData = combatAttackData;
526 }
527
528 public Player getPlayer() {
529 return player;
530 }
531
532 public void setPlayer(Player player) {
533 this.player = player;
534 }
535
536 public Optional<CombatAttackData> getCombatAttackData() {
537 return Optional.ofNullable(combatAttackData);
538 }
539
540 @Override
541 public int hashCode() {
542 return Objects.hash(id, name, size);
543 }
544
545 @Override
546 public boolean equals(Object obj) {
547 return obj == this || (obj instanceof NpcDefinition && ((NpcDefinition) obj).id == id);
548 }
549
550 public static final class CombatAttackData {
551
552 public final CombatType type;
553
554 public final String key;
555
556 public CombatAttackData(CombatType type, String key) {
557 this.type = type;
558 this.key = key;
559 }
560
561 public CombatProjectile getDefinition() {
562 return CombatProjectile.getDefinition(key);
563 }
564 }
565}
static final int NPC_DEFINITION_LIMIT
Definition Config.java:216