Source: globals.js

  1. /**
  2. * Enum for ActionType
  3. * @readonly
  4. * @enum {number}
  5. */
  6. const ActionType = {
  7. NONE : 0,
  8. MOVE : 1,
  9. HARVEST : 2,
  10. ATTACK : 3,
  11. TRANSFER: 4,
  12. PHEROMONE: 5
  13. };
  14. /**
  15. * Enum for DirectionType
  16. * @readonly
  17. * @enum {number}
  18. */
  19. const DirectionType = {
  20. FORWARD: 1,
  21. BACKWARD: 2,
  22. NONE: 3
  23. };
  24. /**
  25. * Enum for PheromoneType
  26. * @readonly
  27. * @enum {number}
  28. */
  29. const PheromoneType = {
  30. NONE : 0,
  31. ATTACK : 1,
  32. DEFEND : 2,
  33. FOOD : 3,
  34. DANGER: 4
  35. };
  36. /**
  37. * Enum for ObjectType
  38. * @readonly
  39. * @enum {number}
  40. */
  41. const ObjectType = {
  42. NONE : 0,
  43. HIVE : 1,
  44. FOOD : 2,
  45. ANT : 3,
  46. SPIDER : 4,
  47. PHEROMONE : 5
  48. };
  49. const ShapeType = {
  50. CIRCLE : 1,
  51. SQUARE : 2,
  52. RECTANGLE : 3
  53. };
  54. const AntType = {
  55. SIMPLE: 0,
  56. CUSTOM: 1
  57. }
  58. const HiveType = {
  59. DEFAULT : 0,
  60. CUSTOM : 1
  61. }
  62. const _FILL_STYLE_TABLE = ['#000000','#ff0000','#00ff00','#0000ff']; // Ant color per hive
  63. /**
  64. * Returns a random value between min and max.
  65. * @param {number} min - Lower threshold.
  66. * @param {number} max - Upper threshold.
  67. * @return {number} random number.
  68. */
  69. function rand(min, max){
  70. return Math.random() * (max - min) + min;
  71. }
  72. /**
  73. * Returns a number converted from radians to degree.
  74. * @param {number} degree.
  75. * @return {number} radians.
  76. */
  77. function degToRad(degrees){
  78. return degrees * Math.PI / 180;
  79. }
  80. /**
  81. * Returns a number converted from degree to radians.
  82. * @param {number} radians.
  83. * @return {number} degree.
  84. */
  85. function radToDeg(radians){
  86. return radians * 180 / Math.PI;
  87. }
  88. /**
  89. * Gets the distance between to points.
  90. * @param {dict} Point a.
  91. * @param {dict} Point b.
  92. * @return {number} distance.
  93. */
  94. function getDistance(a, b){
  95. var dx = (a.x-b.x);
  96. var dy = (a.y-b.y);
  97. return Math.sqrt(dx*dx+dy*dy);
  98. }
  99. /**
  100. * Gets the angle in radians between to vectors [-PI,PI].
  101. * @param {dict} Vector a.
  102. * @param {dict} Vector b.
  103. * @return {number} angle in radians.
  104. */
  105. function angleBetweenVectorsRad(fromVec, toVec){
  106. // reference to math behind this: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/issues/index.htm
  107. var nFromVec = normalize(fromVec);
  108. var nToVec = normalize(toVec);
  109. var angle = Math.atan2(nToVec.y,nToVec.x) - Math.atan2(nFromVec.y,nFromVec.x);
  110. if (angle >= Math.PI)
  111. angle -= Math.PI*2;
  112. else if (angle <= -Math.PI)
  113. angle += Math.PI*2;
  114. return angle;
  115. }
  116. /**
  117. * Gets the angle in degree between to vectors [-180,180].
  118. * @param {dict} Vector a.
  119. * @param {dict} Vector b.
  120. * @return {number} angle in degree.
  121. */
  122. function angleBetweenVectorsDeg(fromVec, toVec){
  123. return radToDeg(angleBetweenVectorsRad(fromVec,toVec));
  124. }
  125. /**
  126. * Rotates a vector by given radians.
  127. * @param {dict} Vector.
  128. * @param {number} Rotation given in radians.
  129. * @return {dict} Rotated vector.
  130. */
  131. function rotateVector(vec, radians)
  132. {
  133. var xNew = vec.x * Math.cos(radians) - vec.y * Math.sin(radians);
  134. var yNew = vec.x * Math.sin(radians) + vec.y * Math.cos(radians);
  135. return { x: xNew, y: yNew };
  136. }
  137. /**
  138. * Gets the normalized vector.
  139. * @param {dict} Vector.
  140. * @return {dict} Normalized vector.
  141. */
  142. function normalize(vec){
  143. var length = getDistance({x:0,y:0}, vec);
  144. var normalizedVec = {x: vec.x/length, y: vec.y/length};
  145. return normalizedVec;
  146. }
  147. /**
  148. * Gets the position inside a list of the maximum value.
  149. * @param {list} List of numbers.
  150. * @return {number} Position of the maximum value.
  151. */
  152. function argmax(tlist) {
  153. var max = -9e8;
  154. var maxarg = -1;
  155. for (var i = 0; i < tlist.length; ++i) {
  156. if (tlist[i] > max) {
  157. max = tlist[i];
  158. maxarg = i;
  159. }
  160. }
  161. return maxarg;
  162. }
  163. /**
  164. * Gets the maximum value in a list.
  165. * @param {list} List of numbers.
  166. * @return {number} Maximum value.
  167. */
  168. function maxElement(tlist) {
  169. var max = -9e8;
  170. for (var i = 0; i < tlist.length; ++i) {
  171. if (tlist[i] > max) {
  172. max = tlist[i];
  173. }
  174. }
  175. return max;
  176. }
  177. function createEditor(elementID, defaultValue){
  178. var antControllerWordCompleter = {
  179. getCompletions: function(editor, session, pos, prefix, callback) {
  180. var wordList = AntController.getAutoCompletionWordList();
  181. callback(null, wordList.map(function(word) {
  182. return {
  183. caption: word,
  184. value: word,
  185. meta: "This ant"
  186. };
  187. }));
  188. }
  189. }
  190. var globalWordCompleter = {
  191. getCompletions: function(editor, session, pos, prefix, callback) {
  192. var wordList = ["this."];
  193. callback(null, wordList.map(function(word) {
  194. return {
  195. caption: word,
  196. value: word,
  197. meta: "global"
  198. };
  199. }));
  200. }
  201. }
  202. ace.require("ace/ext/language_tools");
  203. var customAntEditor = ace.edit(elementID);
  204. customAntEditor.$blockScrolling = Infinity;
  205. customAntEditor.setTheme("ace/theme/chrome");
  206. customAntEditor.session.setMode("ace/mode/javascript");
  207. customAntEditor.setOptions({
  208. enableBasicAutocompletion: true,
  209. enableLiveAutocompletion: true
  210. });
  211. customAntEditor.completers = [globalWordCompleter, antControllerWordCompleter];
  212. customAntEditor.setValue(defaultValue, -1); // -1 set cursor to begin
  213. return customAntEditor;
  214. }