armark1ng

Untitled

Jun 11th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.97 KB | None | 0 0
  1. package com.rs.utils;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.math.BigInteger;
  10. import java.net.URL;
  11. import java.nio.channels.FileChannel;
  12. import java.security.MessageDigest;
  13. import java.security.SecureRandom;
  14. import java.text.DecimalFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. import java.util.Enumeration;
  19. import java.util.List;
  20.  
  21. import com.rs.cache.Cache;
  22. import com.rs.cache.loaders.ObjectDefinitions;
  23. import com.rs.executor.WorldThread;
  24. import com.rs.game.Entity;
  25. import com.rs.game.World;
  26. import com.rs.game.WorldObject;
  27. import com.rs.game.WorldTile;
  28.  
  29. public final class Utils {
  30.  
  31. private static final Object ALGORITHM_LOCK = new Object();
  32.  
  33. private static final long INIT_MILLIS = System.currentTimeMillis();
  34. private static final long INIT_NANOS = System.nanoTime();
  35.  
  36. private static SecureRandom SECURE_RANDOM;
  37.  
  38. static {
  39. try {
  40. // native is too slow
  41. SECURE_RANDOM = SecureRandom.getInstance("SHA1PRNG", "SUN");
  42. } catch (Throwable e) {
  43. Logger.handle(e);
  44. }
  45. }
  46.  
  47. private static long millisSinceClassInit() {
  48. return (System.nanoTime() - INIT_NANOS) / 1000000;
  49. }
  50.  
  51. public static long currentTimeMillis() {
  52. return INIT_MILLIS + millisSinceClassInit();
  53. }
  54.  
  55. /*
  56. * world cycles, each is 600ms :). its 100% safe to use :p example of usage
  57. * well doesnt save with restarts it should work fine for disabled. its bad
  58. * dont use for things that save ofc good for stuff that doesnt save such as
  59. * temporary args and delays
  60. */
  61. public static long currentWorldCycle() {
  62. return WorldThread.WORLD_CYCLE;
  63. }
  64.  
  65. /*
  66. * private static long timeCorrection; private static long lastTimeUpdate;
  67. *
  68. * public static synchronized long currentTimeMillis() { long l =
  69. * System.currentTimeMillis(); if (l < lastTimeUpdate) timeCorrection +=
  70. * lastTimeUpdate - l; lastTimeUpdate = l; return l + timeCorrection; }
  71. */
  72.  
  73. private static final DecimalFormat dFormatter = new DecimalFormat("#,###,###,###");
  74.  
  75. public static String getFormattedNumber(int amount) {
  76. return dFormatter.format(amount);
  77. }
  78.  
  79. public static int getMapArchiveId(int regionX, int regionY) {
  80. return regionX | regionY << 7;
  81. }
  82.  
  83. public static String formatTime(long time) {
  84. long seconds = time / 1000;
  85. long minutes = seconds / 60;
  86. long hours = minutes / 60;
  87. seconds = seconds % 60;
  88. minutes = minutes % 60;
  89. hours = hours % 24;
  90. StringBuilder string = new StringBuilder();
  91. string.append(hours > 9 ? hours : ("0" + hours));
  92. string.append(":" + (minutes > 9 ? minutes : ("0" + minutes)));
  93. string.append(":" + (seconds > 9 ? seconds : ("0" + seconds)));
  94. return string.toString();
  95. }
  96.  
  97. public static void shuffle(int[] array) {
  98. int count = array.length;
  99. for (int i = count; i > 1; i--)
  100. swap(array, i - 1, SECURE_RANDOM.nextInt(i));
  101. }
  102.  
  103. private static void swap(int[] array, int i, int j) {
  104. int temp = array[i];
  105. array[i] = array[j];
  106. array[j] = temp;
  107. }
  108.  
  109. public static String getFormattedNumber(double amount, char seperator) {
  110. String str = new DecimalFormat("#,###,###").format(amount);
  111. char[] rebuff = new char[str.length()];
  112. for (int i = 0; i < str.length(); i++) {
  113. char c = str.charAt(i);
  114. if (c >= '0' && c <= '9')
  115. rebuff[i] = c;
  116. else
  117. rebuff[i] = seperator;
  118. }
  119. return new String(rebuff);
  120. }
  121.  
  122. public static byte[] cryptRSA(byte[] data, BigInteger exponent, BigInteger modulus) {
  123. return new BigInteger(data).modPow(exponent, modulus).toByteArray();
  124. }
  125.  
  126. public static final byte[] encryptUsingMD5(byte[] buffer) {
  127. // prevents concurrency problems with the algorithm
  128. synchronized (ALGORITHM_LOCK) {
  129. try {
  130. MessageDigest algorithm = MessageDigest.getInstance("MD5");
  131. algorithm.update(buffer);
  132. byte[] digest = algorithm.digest();
  133. algorithm.reset();
  134. return digest;
  135. } catch (Throwable e) {
  136. Logger.handle(e);
  137. }
  138. return null;
  139. }
  140. }
  141.  
  142. public static boolean inCircle(WorldTile location, WorldTile center, int radius) {
  143. return getDistance(center, location) < radius;
  144. }
  145.  
  146. public static WorldTile getFreeTile(WorldTile center, int distance) {
  147. WorldTile tile = center;
  148. for (int i = 0; i < 10; i++) {
  149. tile = new WorldTile(center, distance);
  150. if (World.isTileFree(tile.getPlane(), tile.getX(), tile.getY(), 1))
  151. return tile;
  152. }
  153. return center;
  154. }
  155.  
  156. public static void copyFile(File sourceFile, File destFile) throws IOException {
  157. if (!destFile.exists()) {
  158. destFile.createNewFile();
  159. }
  160.  
  161. FileChannel source = null;
  162. FileChannel destination = null;
  163. try {
  164. source = new FileInputStream(sourceFile).getChannel();
  165. destination = new FileOutputStream(destFile).getChannel();
  166. destination.transferFrom(source, 0, source.size());
  167. } finally {
  168. if (source != null) {
  169. source.close();
  170. }
  171. if (destination != null) {
  172. destination.close();
  173. }
  174. }
  175. }
  176.  
  177. @SuppressWarnings({ "rawtypes" })
  178. public static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException {
  179. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  180. assert classLoader != null;
  181. String path = packageName.replace('.', '/');
  182. Enumeration<URL> resources = classLoader.getResources(path);
  183. List<File> dirs = new ArrayList<File>();
  184. while (resources.hasMoreElements()) {
  185. URL resource = resources.nextElement();
  186. dirs.add(new File(resource.getFile().replaceAll("%20", " ")));
  187. }
  188. ArrayList<Class> classes = new ArrayList<Class>();
  189. for (File directory : dirs) {
  190. classes.addAll(findClasses(directory, packageName));
  191. }
  192. return classes.toArray(new Class[classes.size()]);
  193. }
  194.  
  195. @SuppressWarnings("rawtypes")
  196. private static List<Class> findClasses(File directory, String packageName) {
  197. List<Class> classes = new ArrayList<Class>();
  198. if (!directory.exists()) {
  199. return classes;
  200. }
  201. File[] files = directory.listFiles();
  202. for (File file : files) {
  203. if (file.isDirectory()) {
  204. assert !file.getName().contains(".");
  205. classes.addAll(findClasses(file, packageName + "." + file.getName()));
  206. } else if (file.getName().endsWith(".class")) {
  207. try {
  208. classes.add(Class.forName(packageName + '.'
  209. + file.getName().substring(0, file.getName().length() - 6)));
  210. } catch (Throwable e) {
  211.  
  212. }
  213. }
  214. }
  215. return classes;
  216. }
  217.  
  218. public static final int getDistance(WorldTile t1, WorldTile t2) {
  219. return getDistance(t1.getX(), t1.getY(), t2.getX(), t2.getY());
  220. }
  221.  
  222. public static final int getDistance(int coordX1, int coordY1, int coordX2, int coordY2) {
  223. int deltaX = coordX2 - coordX1;
  224. int deltaY = coordY2 - coordY1;
  225. return ((int) Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
  226. }
  227.  
  228. public static final int getMoveDirection(int xOffset, int yOffset) {
  229. if (xOffset < 0) {
  230. if (yOffset < 0)
  231. return 5;
  232. else if (yOffset > 0)
  233. return 0;
  234. else
  235. return 3;
  236. } else if (xOffset > 0) {
  237. if (yOffset < 0)
  238. return 7;
  239. else if (yOffset > 0)
  240. return 2;
  241. else
  242. return 4;
  243. } else {
  244. if (yOffset < 0)
  245. return 6;
  246. else if (yOffset > 0)
  247. return 1;
  248. else
  249. return -1;
  250. }
  251. }
  252.  
  253. public static final String[] NUMBERS = { "one", "two", "three", "four", "five" };
  254.  
  255. public static final byte[] DIRECTION_DELTA_X = new byte[] { -1, 0, 1, -1, 1, -1, 0, 1 };
  256. public static final byte[] DIRECTION_DELTA_Y = new byte[] { 1, 1, 1, 0, 0, -1, -1, -1 };
  257.  
  258. private static final byte[][] ANGLE_DIRECTION_DELTA = { { 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, 1 },
  259. { 1, 1 }, { 1, 0 }, { 1, -1 } };
  260.  
  261. public static int getNpcMoveDirection(int dd) {
  262. return getNpcMoveDirection(DIRECTION_DELTA_X[dd], DIRECTION_DELTA_Y[dd]);
  263. }
  264.  
  265. public static byte[] getDirection(int angle) {
  266. int v = angle >> 11;
  267. return ANGLE_DIRECTION_DELTA[v];
  268. }
  269.  
  270. public static final int getAngle(int xOffset, int yOffset) {
  271. return ((int) (Math.atan2(-xOffset, -yOffset) * 2607.5945876176133)) & 0x3fff;
  272. }
  273.  
  274. public static int getNpcMoveDirection(int dx, int dy) {
  275. if (dx == 0 && dy > 0)
  276. return 0;
  277. if (dx > 0 && dy > 0)
  278. return 1;
  279. if (dx > 0 && dy == 0)
  280. return 2;
  281. if (dx > 0 && dy < 0)
  282. return 3;
  283. if (dx == 0 && dy < 0)
  284. return 4;
  285. if (dx < 0 && dy < 0)
  286. return 5;
  287. if (dx < 0 && dy == 0)
  288. return 6;
  289. if (dx < 0 && dy > 0)
  290. return 7;
  291. return -1;
  292. }
  293.  
  294. public static final int[][] getCoordOffsetsNear(int size) {
  295. int[] xs = new int[4 + (4 * size)];
  296. int[] xy = new int[xs.length];
  297. xs[0] = -size;
  298. xy[0] = 1;
  299. xs[1] = 1;
  300. xy[1] = 1;
  301. xs[2] = -size;
  302. xy[2] = -size;
  303. xs[3] = 1;
  304. xy[2] = -size;
  305. for (int fakeSize = size; fakeSize > 0; fakeSize--) {
  306. xs[(4 + ((size - fakeSize) * 4))] = -fakeSize + 1;
  307. xy[(4 + ((size - fakeSize) * 4))] = 1;
  308. xs[(4 + ((size - fakeSize) * 4)) + 1] = -size;
  309. xy[(4 + ((size - fakeSize) * 4)) + 1] = -fakeSize + 1;
  310. xs[(4 + ((size - fakeSize) * 4)) + 2] = 1;
  311. xy[(4 + ((size - fakeSize) * 4)) + 2] = -fakeSize + 1;
  312. xs[(4 + ((size - fakeSize) * 4)) + 3] = -fakeSize + 1;
  313. xy[(4 + ((size - fakeSize) * 4)) + 3] = -size;
  314. }
  315. return new int[][] { xs, xy };
  316. }
  317.  
  318. public static final int getGraphicDefinitionsSize() {
  319. int lastArchiveId = Cache.STORE.getIndexes()[21].getLastArchiveId();
  320. return lastArchiveId * 256 + Cache.STORE.getIndexes()[21].getValidFilesCount(lastArchiveId);
  321. }
  322.  
  323. public static final int getAnimationDefinitionsSize() {
  324. int lastArchiveId = Cache.STORE.getIndexes()[20].getLastArchiveId();
  325. return lastArchiveId * 128 + Cache.STORE.getIndexes()[20].getValidFilesCount(lastArchiveId);
  326. }
  327.  
  328. public static final int getConfigDefinitionsSize() {
  329. int lastArchiveId = Cache.STORE.getIndexes()[22].getLastArchiveId();
  330. return lastArchiveId * 256 + Cache.STORE.getIndexes()[22].getValidFilesCount(lastArchiveId);
  331. }
  332.  
  333. public static final int getObjectDefinitionsSize() {
  334. int lastArchiveId = Cache.STORE.getIndexes()[16].getLastArchiveId();
  335. return lastArchiveId * 256 + Cache.STORE.getIndexes()[16].getValidFilesCount(lastArchiveId);
  336. }
  337.  
  338. public static final int getNPCDefinitionsSize() {
  339. int lastArchiveId = Cache.STORE.getIndexes()[18].getLastArchiveId();
  340. return lastArchiveId * 128 + Cache.STORE.getIndexes()[18].getValidFilesCount(lastArchiveId);
  341. }
  342.  
  343. // 22314
  344.  
  345. public static final int getItemDefinitionsSize() {
  346. int lastArchiveId = Cache.STORE.getIndexes()[19].getLastArchiveId();
  347. return (lastArchiveId * 256 + Cache.STORE.getIndexes()[19].getValidFilesCount(lastArchiveId));
  348. }
  349.  
  350. public static boolean itemExists(int id) {// cuz this
  351. if (id >= getItemDefinitionsSize()) // setted because of custom items
  352. return false;
  353. return Cache.STORE.getIndexes()[19].fileExists(id >>> 8, 0xff & id);
  354. }
  355.  
  356. public static boolean csMapExists(int scriptId) {
  357. return Cache.STORE.getIndexes()[17].fileExists(scriptId >>> 0xba9ed5a8, scriptId & 0xff);
  358. }
  359.  
  360. public static final int getInterfaceDefinitionsSize() {
  361. return Cache.STORE.getIndexes()[3].getLastArchiveId() + 1;
  362. }
  363.  
  364. public static final int getInterfaceDefinitionsComponentsSize(int interfaceId) {
  365. return Cache.STORE.getIndexes()[3].getLastFileId(interfaceId) + 1;
  366. }
  367.  
  368. /*
  369. * Use random instead
  370. */
  371. @Deprecated
  372. public static final int getRandom(int maxValue) {
  373. return (int) (SECURE_RANDOM.nextDouble() * (maxValue + 1));
  374. }
  375.  
  376. /*
  377. * Use random instead
  378. */
  379. @Deprecated
  380. public static final double getRandomDouble(double maxValue) {
  381. return (SECURE_RANDOM.nextDouble() * (maxValue + 1));
  382. }
  383.  
  384. public static final int random(int min, int max) {
  385. final int n = Math.abs(max - min);
  386. return Math.min(min, max) + (n == 0 ? 0 : random(n));
  387. }
  388.  
  389. public static final double random(double min, double max) {
  390. final double n = Math.abs(max - min);
  391. return Math.min(min, max) + (n == 0 ? 0 : random((int) n));
  392. }
  393.  
  394. public static final int next(int max, int min) {
  395. return min + (int) (SECURE_RANDOM.nextDouble() * ((max - min) + 1));
  396. }
  397.  
  398. public static final int random(int maxValue) {
  399. if (maxValue <= 0)
  400. return 0;
  401. return SECURE_RANDOM.nextInt(maxValue);
  402. }
  403.  
  404. public static final double random(double maxValue) {
  405. return SECURE_RANDOM.nextDouble() * maxValue;
  406. }
  407.  
  408. public static final double randomDouble() {
  409. return SECURE_RANDOM.nextDouble();
  410. }
  411.  
  412. public static final char[] VALID_CHARS = { '_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  413. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  414. '8', '9' };
  415.  
  416. public static boolean invalidAccountName(String name) {
  417. return name.length() < 2 || name.length() > 12 || name.startsWith("_") || name.endsWith("_")
  418. || name.contains("__") || containsInvalidCharacter(name);
  419. }
  420.  
  421. public static boolean containsInvalidCharacter(char c) {
  422. for (char vc : VALID_CHARS) {
  423. if (vc == c)
  424. return false;
  425. }
  426. return true;
  427. }
  428.  
  429. public static boolean containsInvalidCharacter(String name) {
  430. for (char c : name.toCharArray()) {
  431. if (containsInvalidCharacter(c))
  432. return true;
  433. }
  434. return false;
  435. }
  436.  
  437. public static final long stringToLong(String s) {
  438. long l = 0L;
  439. for (int i = 0; i < s.length() && i < 12; i++) {
  440. char c = s.charAt(i);
  441. l *= 37L;
  442. if (c >= 'A' && c <= 'Z')
  443. l += (1 + c) - 65;
  444. else if (c >= 'a' && c <= 'z')
  445. l += (1 + c) - 97;
  446. else if (c >= '0' && c <= '9')
  447. l += (27 + c) - 48;
  448. }
  449. while (l % 37L == 0L && l != 0L) {
  450. l /= 37L;
  451. }
  452. return l;
  453. }
  454.  
  455. public static final String longToString(long l) {
  456. if (l <= 0L || l >= 0x5b5b57f8a98a5dd1L)
  457. return null;
  458. if (l % 37L == 0L)
  459. return null;
  460. int i = 0;
  461. char ac[] = new char[12];
  462. while (l != 0L) {
  463. long l1 = l;
  464. l /= 37L;
  465. ac[11 - i++] = VALID_CHARS[(int) (l1 - l * 37L)];
  466. }
  467. return new String(ac, 12 - i, i);
  468. }
  469.  
  470. /*
  471. * dont use as it blocks
  472. */
  473. public static String getExternalIP() {
  474. try {
  475. URL whatismyip = new URL("http://checkip.amazonaws.com");
  476. BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
  477. String ip = in.readLine(); // you get the IP as a String
  478. in.close();
  479. return ip;
  480. } catch (Throwable e) {
  481. return "127.0.0.1";
  482. }
  483. }
  484.  
  485. public static final int getNameHash(String name) {
  486. name = name.toLowerCase();
  487. int hash = 0;
  488. for (int index = 0; index < name.length(); index++)
  489. hash = method1258(name.charAt(index)) + ((hash << 5) - hash);
  490. return hash;
  491. }
  492.  
  493. public static final byte method1258(char c) {
  494. byte charByte;
  495. if (c > 0 && c < '\200' || c >= '\240' && c <= '\377') {
  496. charByte = (byte) c;
  497. } else if (c != '\u20AC') {
  498. if (c != '\u201A') {
  499. if (c != '\u0192') {
  500. if (c == '\u201E') {
  501. charByte = -124;
  502. } else if (c != '\u2026') {
  503. if (c != '\u2020') {
  504. if (c == '\u2021') {
  505. charByte = -121;
  506. } else if (c == '\u02C6') {
  507. charByte = -120;
  508. } else if (c == '\u2030') {
  509. charByte = -119;
  510. } else if (c == '\u0160') {
  511. charByte = -118;
  512. } else if (c == '\u2039') {
  513. charByte = -117;
  514. } else if (c == '\u0152') {
  515. charByte = -116;
  516. } else if (c != '\u017D') {
  517. if (c == '\u2018') {
  518. charByte = -111;
  519. } else if (c != '\u2019') {
  520. if (c != '\u201C') {
  521. if (c == '\u201D') {
  522. charByte = -108;
  523. } else if (c != '\u2022') {
  524. if (c == '\u2013') {
  525. charByte = -106;
  526. } else if (c == '\u2014') {
  527. charByte = -105;
  528. } else if (c == '\u02DC') {
  529. charByte = -104;
  530. } else if (c == '\u2122') {
  531. charByte = -103;
  532. } else if (c != '\u0161') {
  533. if (c == '\u203A') {
  534. charByte = -101;
  535. } else if (c != '\u0153') {
  536. if (c == '\u017E') {
  537. charByte = -98;
  538. } else if (c != '\u0178') {
  539. charByte = 63;
  540. } else {
  541. charByte = -97;
  542. }
  543. } else {
  544. charByte = -100;
  545. }
  546. } else {
  547. charByte = -102;
  548. }
  549. } else {
  550. charByte = -107;
  551. }
  552. } else {
  553. charByte = -109;
  554. }
  555. } else {
  556. charByte = -110;
  557. }
  558. } else {
  559. charByte = -114;
  560. }
  561. } else {
  562. charByte = -122;
  563. }
  564. } else {
  565. charByte = -123;
  566. }
  567. } else {
  568. charByte = -125;
  569. }
  570. } else {
  571. charByte = -126;
  572. }
  573. } else {
  574. charByte = -128;
  575. }
  576. return charByte;
  577. }
  578.  
  579. public static String formatPlayerNameForProtocol(String name) {
  580. if (name == null)
  581. return "";
  582. name = name.replaceAll(" ", "_");
  583. name = name.toLowerCase();
  584. return name;
  585. }
  586.  
  587. public static String formatPlayerNameForDisplay(String name) {
  588. if (name == null)
  589. return "";
  590. name = name.replaceAll("_", " ");
  591. name = name.toLowerCase();
  592. StringBuilder newName = new StringBuilder();
  593. boolean wasSpace = true;
  594. for (int i = 0; i < name.length(); i++) {
  595. if (wasSpace) {
  596. newName.append(("" + name.charAt(i)).toUpperCase());
  597. wasSpace = false;
  598. } else {
  599. newName.append(name.charAt(i));
  600. }
  601. if (name.charAt(i) == ' ') {
  602. wasSpace = true;
  603. }
  604. }
  605. return newName.toString();
  606. }
  607.  
  608. private static final char[] UNICODE_TABLE = { '\u20ac', '\0', '\u201a', '\u0192', '\u201e', '\u2026', '\u2020',
  609. '\u2021', '\u02c6', '\u2030', '\u0160', '\u2039', '\u0152', '\0', '\u017d', '\0', '\0', '\u2018', '\u2019',
  610. '\u201c', '\u201d', '\u2022', '\u2013', '\u2014', '\u02dc', '\u2122', '\u0161', '\u203a', '\u0153', '\0',
  611. '\u017e', '\u0178' };
  612.  
  613. public static char method2782(byte value) {
  614. int byteChar = 0xff & value;
  615. if (byteChar == 0)
  616. throw new IllegalArgumentException("Non cp1252 character 0x" + Integer.toString(byteChar, 16) + " provided");
  617. if ((byteChar ^ 0xffffffff) <= -129 && byteChar < 160) {
  618. int i_4_ = UNICODE_TABLE[-128 + byteChar];
  619. if ((i_4_ ^ 0xffffffff) == -1)
  620. i_4_ = 63;
  621. byteChar = i_4_;
  622. }
  623. return (char) byteChar;
  624. }
  625.  
  626. public static int getHashMapSize(int size) {
  627. size--;
  628. size |= size >>> -1810941663;
  629. size |= size >>> 2010624802;
  630. size |= size >>> 10996420;
  631. size |= size >>> 491045480;
  632. size |= size >>> 1388313616;
  633. return 1 + size;
  634. }
  635.  
  636. /**
  637. * Walk dirs 0 - South-West 1 - South 2 - South-East 3 - West 4 - East 5 -
  638. * North-West 6 - North 7 - North-East
  639. */
  640. public static int getPlayerWalkingDirection(int dx, int dy) {
  641. if (dx == -1 && dy == -1) {
  642. return 0;
  643. }
  644. if (dx == 0 && dy == -1) {
  645. return 1;
  646. }
  647. if (dx == 1 && dy == -1) {
  648. return 2;
  649. }
  650. if (dx == -1 && dy == 0) {
  651. return 3;
  652. }
  653. if (dx == 1 && dy == 0) {
  654. return 4;
  655. }
  656. if (dx == -1 && dy == 1) {
  657. return 5;
  658. }
  659. if (dx == 0 && dy == 1) {
  660. return 6;
  661. }
  662. if (dx == 1 && dy == 1) {
  663. return 7;
  664. }
  665. return -1;
  666. }
  667.  
  668. public static int getPlayerRunningDirection(int dx, int dy) {
  669. if (dx == -2 && dy == -2)
  670. return 0;
  671. if (dx == -1 && dy == -2)
  672. return 1;
  673. if (dx == 0 && dy == -2)
  674. return 2;
  675. if (dx == 1 && dy == -2)
  676. return 3;
  677. if (dx == 2 && dy == -2)
  678. return 4;
  679. if (dx == -2 && dy == -1)
  680. return 5;
  681. if (dx == 2 && dy == -1)
  682. return 6;
  683. if (dx == -2 && dy == 0)
  684. return 7;
  685. if (dx == 2 && dy == 0)
  686. return 8;
  687. if (dx == -2 && dy == 1)
  688. return 9;
  689. if (dx == 2 && dy == 1)
  690. return 10;
  691. if (dx == -2 && dy == 2)
  692. return 11;
  693. if (dx == -1 && dy == 2)
  694. return 12;
  695. if (dx == 0 && dy == 2)
  696. return 13;
  697. if (dx == 1 && dy == 2)
  698. return 14;
  699. if (dx == 2 && dy == 2)
  700. return 15;
  701. return -1;
  702. }
  703.  
  704. public static String fixChatMessage(String message) {
  705. StringBuilder newText = new StringBuilder();
  706. boolean wasSpace = true;
  707. boolean exception = false;
  708. for (int i = 0; i < message.length(); i++) {
  709. if (!exception) {
  710. if (wasSpace) {
  711. newText.append(("" + message.charAt(i)).toUpperCase());
  712. if (!String.valueOf(message.charAt(i)).equals(" "))
  713. wasSpace = false;
  714. } else {
  715. newText.append(("" + message.charAt(i)).toLowerCase());
  716. }
  717. } else {
  718. newText.append(("" + message.charAt(i)));
  719. }
  720. if (String.valueOf(message.charAt(i)).contains(":"))
  721. exception = true;
  722. else if (String.valueOf(message.charAt(i)).contains(".") || String.valueOf(message.charAt(i)).contains("!")
  723. || String.valueOf(message.charAt(i)).contains("?"))
  724. wasSpace = true;
  725. }
  726. return newText.toString();
  727. }
  728.  
  729. public static final int[] ROTATION_DIR_X = { -1, 0, 1, 0 };
  730.  
  731. public static final int[] ROTATION_DIR_Y = { 0, 1, 0, -1 };
  732.  
  733. private Utils() {
  734.  
  735. }
  736.  
  737. public static String currentTime(String dateFormat) {
  738. Calendar cal = Calendar.getInstance();
  739. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  740. return sdf.format(cal.getTime());
  741. }
  742.  
  743. public static boolean colides(int x1, int y1, int size1, int x2, int y2, int size2) {
  744. int distanceX = x1 - x2;
  745. int distanceY = y1 - y2;
  746. return distanceX < size2 && distanceX > -size1 && distanceY < size2 && distanceY > -size1;
  747. }
  748.  
  749. public static boolean isOnRange(int x1, int y1, int size1, int x2, int y2, int size2, int maxDistance) {
  750. int distanceX = x1 - x2;
  751. int distanceY = y1 - y2;
  752. if (distanceX > size2 + maxDistance || distanceX < -size1 - maxDistance || distanceY > size2 + maxDistance
  753. || distanceY < -size1 - maxDistance)
  754. return false;
  755. return true;
  756. }
  757.  
  758. public static boolean colides(Entity entity, Entity target) {
  759. return entity.getPlane() == target.getPlane()
  760. && colides(entity.getX(), entity.getY(), entity.getSize(), target.getX(), target.getY(),
  761. target.getSize());
  762. }
  763.  
  764. public static boolean colides(WorldTile entity, WorldTile target, int s1, int s2) {
  765. return entity.getPlane() == target.getPlane()
  766. && colides(entity.getX(), entity.getY(), s1, target.getX(), target.getY(), s2);
  767. }
  768.  
  769. public static boolean isOnRange(Entity entity, Entity target, int rangeRatio) {
  770. return entity.getPlane() == target.getPlane()
  771. && isOnRange(entity.getX(), entity.getY(), entity.getSize(), target.getX(), target.getY(),
  772. target.getSize(), rangeRatio);
  773. }
  774.  
  775. public static boolean isOnRange(WorldTile entity, WorldTile target, int rangeRatio, int s1, int s2) {
  776. return entity.getPlane() == target.getPlane()
  777. && isOnRange(entity.getX(), entity.getY(), s1, target.getX(), target.getY(), s2, rangeRatio);
  778. }
  779.  
  780. public static double getProjectileSpeed(WorldTile startTile, WorldTile endTile, int startHeight, int endHeight,
  781. long startTime, long arriveTime) {
  782. int fromSizeX, fromSizeY;
  783. if (startTile instanceof Entity)
  784. fromSizeX = fromSizeY = ((Entity) startTile).getSize();
  785. else if (startTile instanceof WorldObject) {
  786. ObjectDefinitions defs = ((WorldObject) startTile).getDefinitions();
  787. fromSizeX = defs.getSizeX();
  788. fromSizeY = defs.getSizeY();
  789. } else
  790. fromSizeX = fromSizeY = 1;
  791. int toSizeX, toSizeY;
  792. if (endTile instanceof Entity)
  793. toSizeX = toSizeY = ((Entity) endTile).getSize();
  794. else if (endTile instanceof WorldObject) {
  795. ObjectDefinitions defs = ((WorldObject) endTile).getDefinitions();
  796. toSizeX = defs.getSizeX();
  797. toSizeY = defs.getSizeY();
  798. } else
  799. toSizeX = toSizeY = 1;
  800. int fromX = startTile.getX() * 2 + fromSizeX;
  801. int fromY = startTile.getY() * 2 + fromSizeY;
  802.  
  803. int toX = endTile.getX() * 2 + toSizeX;
  804. int toY = endTile.getY() * 2 + toSizeY;
  805.  
  806. fromX /= 2;
  807. fromY /= 2;
  808. toX /= 2;
  809. toY /= 2;
  810.  
  811. int deltaX = fromX - toX;
  812. int deltaY = fromY - toY;
  813. double distance = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
  814.  
  815. double speed = ((distance * 1000) / (arriveTime - startTime));
  816.  
  817. return speed;
  818. }
  819.  
  820. public static int getProjectileTime(WorldTile startTile, WorldTile endTile, int startHeight, int endHeight,
  821. int speed, int delay, int curve, int startDistanceOffset, int creatorSize) {
  822. int distance = Utils.getDistance(startTile, endTile) + 1;
  823. if (speed == 0) // cant be 0, happens cuz method wrong and so /10 needed
  824. // so may round to 0
  825. speed = 1;
  826. return (int) ((delay * 10) + (distance * ((30 / speed) * 10)
  827. /**
  828. * Math.cos(Math.toRadians(curve))
  829. */
  830. ));
  831. }
  832.  
  833. public static int getProjectileTimeNew(WorldTile from, int fromSizeX, int fromSizeY, WorldTile to, int toSizeX,
  834. int toSizeY, double speed) {
  835. int fromX = from.getX() * 2 + fromSizeX;
  836. int fromY = from.getY() * 2 + fromSizeY;
  837.  
  838. int toX = to.getX() * 2 + toSizeX;
  839. int toY = to.getY() * 2 + toSizeY;
  840.  
  841. fromX /= 2;
  842. fromY /= 2;
  843. toX /= 2;
  844. toY /= 2;
  845.  
  846. int deltaX = fromX - toX;
  847. int deltaY = fromY - toY;
  848. int sqrt = (int) Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
  849. return (int) (sqrt * (10 / speed));
  850. }
  851.  
  852. public static int getProjectileTimeSoulsplit(WorldTile from, int fromSizeX, int fromSizeY, WorldTile to,
  853. int toSizeX, int toSizeY) {
  854. int fromX = from.getX() * 2 + fromSizeX;
  855. int fromY = from.getY() * 2 + fromSizeY;
  856.  
  857. int toX = to.getX() * 2 + toSizeX;
  858. int toY = to.getY() * 2 + toSizeY;
  859.  
  860. fromX /= 2;
  861. fromY /= 2;
  862. toX /= 2;
  863. toY /= 2;
  864.  
  865. int deltaX = fromX - toX;
  866. int deltaY = fromY - toY;
  867. int sqrt = (int) Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
  868. sqrt *= 15;
  869. sqrt -= sqrt % 30;
  870. return Math.max(30, sqrt);
  871. }
  872.  
  873. public static int projectileTimeToCycles(int time) {
  874. return (time + 29) / 30;
  875. /* return Math.max(1, (time+14)/30); */
  876. }
  877.  
  878. private static final String[] NUMBER_NAMES = { "Zero", "One", "Two", "Three", "Four" };
  879.  
  880. public static String toNumString(int i) {
  881. return NUMBER_NAMES[i];
  882. }
  883. }
Advertisement
Add Comment
Please, Sign In to add comment