RuneHive-Game
Loading...
Searching...
No Matches
PlayerBot.java
Go to the documentation of this file.
1package com.runehive.content.bot;
2
3import com.runehive.Config;
4import com.runehive.content.bot.botclass.BotClass;
5import com.runehive.content.bot.objective.BotObjective;
6import com.runehive.content.consume.PotionData;
7import com.runehive.content.skill.impl.magic.Spellbook;
8import com.runehive.content.skill.impl.magic.teleport.Teleportation;
9import com.runehive.content.skill.impl.magic.teleport.TeleportationData;
10import com.runehive.content.teleport.TeleportTablet;
11import com.runehive.game.Animation;
12import com.runehive.game.UpdatePriority;
13import com.runehive.game.event.impl.ItemClickEvent;
14import com.runehive.game.world.World;
15import com.runehive.game.world.entity.combat.strategy.player.special.CombatSpecial;
16import com.runehive.game.world.entity.mob.Mob;
17import com.runehive.game.world.entity.mob.player.Player;
18import com.runehive.game.world.entity.skill.Skill;
19import com.runehive.game.world.items.Item;
20import com.runehive.util.Utility;
21
22import java.util.concurrent.atomic.AtomicInteger;
23
24/**
25 * The player bot entity.
26 *
27 * @author Daniel.
28 */
29public class PlayerBot extends Player {
30 public static final AtomicInteger BOT_COUNT = new AtomicInteger(0);
31
32 private int ticks;
33 private Runnable action;
34 private boolean loop;
35 public int foodRemaining;
39
40 public int consumableDelay;
41
42 public PlayerBot() {
44 }
45
46 public PlayerBot(String name) {
47 super(name);
48 setVisible(true);
49 isBot = true;
50 headIcon = -1;
51 clanTag = "";
52 clanTagColor = "";
53 newPlayer = false;
54 regionChange = true;
55 positionChange = true;
56 settings.acceptAid = false;
57 settings.autoRetaliate = true;
61 prayer.reset();
62 mobAnimation.reset();
63 playerAssistant.setPrayer();
64 movement.setRunningToggled(true);
65 skills.setCombatLevel();
66 settings.lockExperience = true;
67 CombatSpecial.restore(this, 100);
68 }
69
70 @Override
71 public void register() {
72 if (!isRegistered() && !World.getPlayers().contains(this)) {
73 setRegistered(World.getPlayers().add(this));
75 BOT_COUNT.incrementAndGet();
76 }
77 }
78
79 @Override
80 public void unregister() {
81 if (!isRegistered()) {
82 return;
83 }
84
85 if (!World.getPlayers().contains(this)) {
86 return;
87 }
88
89 World.cancelTask(this, true);
90 World.getPlayers().remove((Player) destroy());
91 BOT_COUNT.decrementAndGet();
92 }
93
94 @Override
95 public void sequence() {
96 super.sequence();
97
98 if (ticks > 0) {
99 ticks--;
100 }
101
102 if (ticks == 0 && action != null) {
103 Runnable tmp = action;
104 action.run();
105 if (!loop && action == tmp)
106 action = null;
107 }
108
109 if (opponent != null) {
110 loopCombat();
111 }
112 }
113
114 public void schedule(int ticks, Runnable action) {
115 this.ticks = ticks;
116 this.action = action;
117 loop = false;
118 }
119
120 public void loop(int ticks, Runnable action) {
121 this.ticks = ticks;
122 this.action = action;
123 loop = true;
124 }
125
126 public void pause(int ticks) {
127 this.ticks += ticks;
128 }
129
130 public void stopLoop() {
131 loop = false;
132 }
133
135 if (!potion.canDrink(this)) {
136 return;
137 }
138
140 potionDelay.reset();
141
142 Item replace = PotionData.getReplacementItem(event.getItem());
143
144 if (replace.getId() == 229) {
145 inventory.remove(event.getItem());
146 } else {
147 inventory.replace(event.getItem().getId(), replace.getId(), event.getSlot(), true);
148 }
149
150 potion.onEffect(this);
151 getCombat().attack(opponent);
152 }
153
154 public void postDeath() {
156 skills.restoreAll();
157 equipment.login();
158 playerAssistant.reset();
159 setSpecialActivated(false);
160 getCombat().getDamageCache().clear();
161 CombatSpecial.restore(this, 100);
162 movement.reset();
163 teleblockTimer.set(0);
164 schedule(2, () -> BotObjective.WALK_TO_BANK.init(this));
165 }
166
167 public void retaliate(Player defender) {
168 if (getCombat().isAttacking() && !getCombat().isAttacking(defender)) {
169 endFight();
170 return;
171 }
172
173 if (defender.getCombat().isUnderAttack() && !defender.getCombat().isUnderAttackBy(this)) {
174 endFight();
175 return;
176 }
177
178 if (opponent != null && opponent.equals(defender)) {
179 return;
180 }
181
182 if (opponent != null && !opponent.equals(defender)) {
183 endFight();
184 return;
185 }
186
187 BotObjective.COMBAT.init(this);
188 }
189
190 private void loopCombat() {
191 if (opponent.isDead()) {
192 schedule(4, this::endFight);
193 return;
194 }
195
196 if (foodRemaining == 0 || skills.getLevel(Skill.PRAYER) == 0) {
197 endFight();
198 return;
199 }
200
201 if (isDead() || getCurrentHealth() <= 0) {
203 schedule(7, () -> BotObjective.WALK_TO_BANK.init(this));
204 opponent = null;
205 getCombat().reset();
206 return;
207 }
208
209 if (!getCombat().inCombat()
210 || (opponent.getCombat().isUnderAttack() && !opponent.getCombat().isUnderAttackBy(this))) {
211 endFight();
212 return;
213 }
214
215 getCombat().attack(opponent);
216
217 if (consumableDelay > 0) {
219 }
220
221 if (consumableDelay == 0) {
222 botClass.pot(opponent, this);
223 botClass.eat(opponent, this);
224 }
225
226 botClass.handleCombat(opponent, this);
227 }
228
229 public void endFight() {
230 unpoison();
231 unvenom();
232 opponent = null;
233 botClass.endFight(this);
234 getCombat().reset();
235 damageImmunity.reset(3_000);
236 if (!isDead() && getCurrentHealth() > 0) {
238 }
239 }
240}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final Position DEFAULT_POSITION
The default, i.e.
Definition Config.java:197
Holds all the constants used by bot.
static final String[] DEATH_MESSAGES
Array of all the possible death message.
static final Appearance APPEARANCE
The default bot appearance.
static String nameGenerator()
Generates a random bot named based on the available names.
static final PlayerTitle TITLE
The default bot title.
void loop(int ticks, Runnable action)
void schedule(int ticks, Runnable action)
void register()
Registers an entity to the World.
void retaliate(Player defender)
void unregister()
Unregisters an entity from the World.
void pot(Mob opponent, ItemClickEvent event, PotionData potion)
static final AtomicInteger BOT_COUNT
static boolean teleport(Player player, Position position)
Class that models a single animation used by an entity.
Represents the game world.
Definition World.java:46
static void cancelTask(Object attachment)
Definition World.java:536
static MobList< Player > getPlayers()
Definition World.java:544
void setVisible(boolean visible)
Definition Entity.java:102
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
Handles the mob class.
Definition Mob.java:66
void speak(String forceChat)
Sets the mob's forced chat.
Definition Mob.java:127
void move(Position position)
Moves the mob to a set position.
Definition Mob.java:340
Combat< Player > getCombat()
The combat of the mob.
Definition Player.java:759
Represents a trainable and usable skill.
Definition Skill.java:18
static final int PRAYER
The prayer skill id.
Definition Skill.java:36
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
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
The enumerated type managing consumable potion types.
abstract void onEffect(Player player)
The method executed when this potion type activated.
static Item getReplacementItem(Item item)
Retrieves the replacement item for item.
boolean canDrink(Player player)
The method which determines if the player can drink the potion.
The in-game spellbooks for players.
Definition Spellbook.java:8
Represents different priorities for updating.
The enumerated type whose elements represent the combat special attacks.
static void restore(Player player, int amount)
Restores the special bar for player.