Untitled


SUBMITTED BY: Guest

DATE: Feb. 4, 2014, 8:48 p.m.

FORMAT: Java

SIZE: 14.5 kB

HITS: 750

  1. package source;
  2. import org.osbot.script.Script;
  3. import org.osbot.script.ScriptManifest;
  4. @ScriptManifest(author = "", info = "", name = "", version = 1.0)
  5. public class Main extends Script {
  6. public Combat combat = new Combat(this);
  7. public int onLoop() {
  8. if(runCombatListener()) {
  9. combat.update();
  10. }
  11. return 300;
  12. }
  13. public boolean runCombatListener() {
  14. return true;
  15. }
  16. }
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.osbot.script.rs2.model.Character;
  20. import org.osbot.script.rs2.model.Entity;
  21. import org.osbot.script.rs2.model.NPC;
  22. import org.osbot.script.rs2.model.Player;
  23. public class Combat {
  24. public Main main;
  25. public List<CombatEntity> combatEntities;
  26. public Combat(Main main) {
  27. this.main = main;
  28. combatEntities = new ArrayList<CombatEntity>();
  29. }
  30. public void update() {
  31. //Clean Up
  32. for(CombatEntity combat : combatEntities) {
  33. if(combat == null) {
  34. combatEntities.remove(combat);
  35. continue;
  36. }
  37. if(!combat.getEntity().exists()) {
  38. if(combat.hasAttacked) {
  39. CombatEntity cleanup = combat.getAttacking();
  40. cleanup.attacker = null;
  41. if(cleanup.attacking == combat) {
  42. cleanup.setAttacking(null);
  43. cleanup.setHasAttacked(false);
  44. }
  45. }
  46. }
  47. }
  48. //add new entities
  49. for(NPC npc : main.client.getLocalNPCs()) {
  50. if(npc == null) {
  51. continue;
  52. }
  53. Entity facing = npc.getFacing();
  54. if(facing != null) {
  55. if(!exists(npc)) {
  56. combatEntities.add(new CombatEntity(npc,facing));
  57. }
  58. } else {
  59. if(exists(npc)) {
  60. CombatEntity remove = getCombatEntity(npc);
  61. combatEntities.remove(remove);
  62. }
  63. }
  64. }
  65. for(Player player : main.client.getLocalPlayers()) {
  66. if(player == null) {
  67. continue;
  68. }
  69. Entity facing = player.getFacing();
  70. if(facing != null) {
  71. if(!exists(player)) {
  72. combatEntities.add(new CombatEntity(player,facing));
  73. }
  74. } else {
  75. if(exists(player)) {
  76. CombatEntity remove = getCombatEntity(player);
  77. combatEntities.remove(remove);
  78. }
  79. }
  80. }
  81. //Updates state of the entities
  82. for(CombatEntity combat : combatEntities) {
  83. if(combat == null) {
  84. combatEntities.remove(combat);
  85. continue;
  86. }
  87. Entity facing = ((Character<?>) combat.getEntity()).getFacing();
  88. if(combat.getFacing() != facing) { //switched to new target
  89. if(combat.getHasAttacked()) {
  90. CombatEntity entity = combat.getAttacking();
  91. combat.setHasAttacked(false);//if switched to new target we do not know if he has fully attacked yet
  92. combat.setFacing(facing);
  93. entity.setAttacker(null);
  94. }
  95. }
  96. if(((Character<?>) combat.getEntity()).getAnimation() != -1) { //made a combat animation (if non combat animation has happened you would no longer be facing a target and wouldn't reach this point)
  97. combat.setHasAttacked(true);
  98. CombatEntity attacking = getCombatEntity(combat.getFacing());
  99. attacking.setAttacker(combat);
  100. combat.setAttacking(attacking);
  101. }
  102. }
  103. }
  104. public boolean attack(int targetId) throws InterruptedException {
  105. Entity target = getClosestTarget(targetId);
  106. if(target == null) {
  107. return false;
  108. }
  109. target.interact("Attack");
  110. return true;
  111. }
  112. public boolean attackRandomWithinDistance(int distance, int targetId) throws InterruptedException {
  113. Entity target = getRandomTarget(distance, targetId);
  114. if(target == null) {
  115. return false;
  116. }
  117. target.interact("Attack");
  118. return true;
  119. }
  120. public boolean canAttack() {
  121. CombatEntity myPlayer = getCombatEntity(main.client.getMyPlayer());
  122. if(myPlayer.isAttacking()) {
  123. return false;
  124. }
  125. if(myPlayer.getFacing() != null) {
  126. CombatEntity facing = getCombatEntity(myPlayer.getFacing());
  127. if(facing.getAttacker() != myPlayer) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. return true;
  133. }
  134. public Entity getRandomTarget(int targetId, int distance) { //will still return if being attacked by an aggressive npc
  135. CombatEntity myPlayer = getCombatEntity(main.client.getMyPlayer());
  136. for(CombatEntity combat : combatEntities) {
  137. if(combat == null) {
  138. combatEntities.remove(combat);
  139. continue;
  140. }
  141. if(combat.getFacing() == main.client.getMyPlayer()) {
  142. if(combat.beingAttacked() && combat.getAttacker() != myPlayer) {
  143. continue;
  144. }
  145. return combat.getEntity(); //If being targetted by a npc in an aggressive area, checks if this npc is being attacked by another player
  146. }
  147. }
  148. List<NPC> attackables = new ArrayList<NPC>();
  149. for(NPC npc : main.client.getLocalNPCs()) {
  150. if(npc == null) {
  151. continue;
  152. }
  153. if(npc.getId() != targetId) {
  154. continue;
  155. }
  156. if(exists(npc)) {
  157. CombatEntity combatEntity = getCombatEntity(npc);
  158. if(combatEntity.beingAttacked()) {
  159. continue;
  160. }
  161. }
  162. int distanceBetween = main.client.getMyPlayer().getPosition().distance(npc.getPosition());
  163. if(distanceBetween < distance) { //checks if this entity is closer or not, if so sets it to the currently known closest npc
  164. attackables.add(npc);
  165. }
  166. }
  167. if(attackables.size() == 0) {
  168. return null;
  169. }
  170. NPC toReturn = attackables.get((int) (Math.random() * (attackables.size()-1)));
  171. return toReturn;
  172. }
  173. public Entity getClosestTarget(int targetId) {
  174. CombatEntity myPlayer = getCombatEntity(main.client.getMyPlayer());
  175. for(CombatEntity combat : combatEntities) {
  176. if(combat == null) {
  177. combatEntities.remove(combat);
  178. continue;
  179. }
  180. if(combat.getFacing() == main.client.getMyPlayer()) {
  181. if(combat.beingAttacked() && combat.getAttacker() != myPlayer) {
  182. continue;
  183. }
  184. return combat.getEntity(); //If being targetted by a npc in an aggressive area, checks if this npc is being attacked by another player
  185. }
  186. }
  187. NPC closestAttackable = null;
  188. int distance = 20; //Max distance it'll attack (change if you want O.o)
  189. for(NPC npc : main.client.getLocalNPCs()) {
  190. if(npc == null) {
  191. continue;
  192. }
  193. if(npc.getId() != targetId) {
  194. continue;
  195. }
  196. if(exists(npc)) {
  197. CombatEntity combatEntity = getCombatEntity(npc);
  198. if(combatEntity.beingAttacked()) {
  199. continue;
  200. }
  201. }
  202. int distanceBetween = main.client.getMyPlayer().getPosition().distance(npc.getPosition());
  203. if(distanceBetween < distance) { //checks if this entity is closer or not, if so sets it to the currently known closest npc
  204. closestAttackable = npc;
  205. distance = distanceBetween;
  206. }
  207. }
  208. return closestAttackable; //if returns null there is no npcs to attack O.o
  209. }
  210. public boolean exists(Entity entity) {
  211. for(CombatEntity check : combatEntities) {
  212. if(check == null) {
  213. combatEntities.remove(check);
  214. continue;
  215. }
  216. if(check.isEntity(entity)) {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. public CombatEntity getCombatEntity(Entity entity) {
  223. for(CombatEntity check : combatEntities) {
  224. if(check == null) {
  225. combatEntities.remove(check);
  226. continue;
  227. }
  228. if(check.isEntity(entity)) {
  229. return check;
  230. }
  231. }
  232. return null;
  233. }
  234. }
  235. import org.osbot.script.rs2.model.Entity;
  236. public class CombatEntity {
  237. public Entity entity;
  238. public Entity facing;
  239. public boolean hasAttacked;
  240. public CombatEntity attacker = null;
  241. public CombatEntity attacking = null;
  242. public CombatEntity(Entity entity, Entity facing) {
  243. this.entity = entity;
  244. this.facing = facing;
  245. }
  246. public boolean isEntity(Entity entity) {
  247. if(this.entity == entity) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. public boolean getHasAttacked() {
  253. return hasAttacked;
  254. }
  255. public void setHasAttacked(boolean bool) {
  256. hasAttacked = bool;
  257. }
  258. public Entity getEntity() {
  259. return entity;
  260. }
  261. public Entity getFacing() {
  262. return facing;
  263. }
  264. public void setFacing(Entity facing) {
  265. this.facing = facing;
  266. }
  267. public CombatEntity getAttacking() {
  268. return attacking;
  269. }
  270. public void setAttacking(CombatEntity entity) {
  271. attacking = entity;
  272. }
  273. public CombatEntity getAttacker() {
  274. return attacker;
  275. }
  276. public void setAttacker(CombatEntity attacker) {
  277. this.attacker = attacker;
  278. }
  279. public boolean beingAttacked() {
  280. if(attacker != null) {
  281. return true;
  282. }
  283. return false;
  284. }
  285. public boolean isAttacking() {
  286. if(attacking != null) {
  287. return true;
  288. }
  289. return false;
  290. }
  291. }

comments powered by Disqus