Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. /***
  2. * Name: PathFinding
  3. * Author: pavel
  4. * Description:
  5. * Tags: Tag1, Tag2, TagN
  6. ***/
  7.  
  8. model PathFinding
  9.  
  10. global {
  11. int size <- 30;
  12. int objects_count <- 3;
  13. int maxDistance <- 0;
  14. init {
  15. // create actor number: objects_count;
  16. create Target number: objects_count;
  17. }
  18. }
  19.  
  20. /*
  21. species actor {
  22.  
  23. float size <- 1.0 ;
  24. int x;
  25. int y;
  26.  
  27. rgb color <- #blue ;
  28. pos_cell myCell <- one_of (pos_cell);
  29.  
  30. init {
  31. location <- myCell.location;
  32. }
  33.  
  34.  
  35. string text;
  36.  
  37. reflex Move {
  38.  
  39. list<pos_cell> neighbs <- (myCell neighbors_at 1);
  40.  
  41. int minDist <- 60;
  42. int minDistIndex <- 0;
  43.  
  44. loop k from: 0 to: length(neighbs)-1 {
  45. if (neighbs[k].weight < minDist)
  46. {
  47. minDist <- neighbs[k].weight;
  48. minDistIndex <- k;
  49. }
  50. }
  51.  
  52. text <- neighbs[minDistIndex].weight;
  53. myCell <- neighbs[minDistIndex];
  54. location <- myCell.location;
  55. }
  56.  
  57.  
  58.  
  59. aspect base {
  60. draw circle(size) color: color ;
  61.  
  62. draw 'Actor ' + text color: #black;
  63. }
  64.  
  65. }
  66.  
  67. */
  68.  
  69. grid pos_cell width: size height: size neighbors: 4 {
  70.  
  71. list<pos_cell> neighbours <- (self neighbors_at 1);
  72. rgb color <- rgb(255,255,255);
  73. int weight <- 0;
  74. bool initialized <- false;
  75. bool hasInitializedNeighbours <- false;
  76. int nextDrawCycle <- 9999999;
  77.  
  78. reflex Initialize when: ((!initialized) and (hasInitializedNeighbours)) and (cycle >= nextDrawCycle)
  79. {
  80. int minWeight <- 99999;
  81. loop i from: 0 to: length (neighbours)
  82. {
  83. if(neighbours[i] != nil)
  84. {
  85. if(neighbours[i].initialized)
  86. {
  87. write("Initialized neigh weight" + neighbours[i].weight);
  88. if(neighbours[i].weight < minWeight)
  89. {
  90. minWeight <- neighbours[i].weight;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. weight <- minWeight + 1;
  97. if(weight > maxDistance)
  98. {
  99. maxDistance <- weight;
  100. }
  101. initialized <- true;
  102. loop i from: 0 to: length (neighbours)
  103. {
  104. if(neighbours[i] != nil)
  105. {
  106. ask(neighbours[i]) { do SetHasInitializedNeighbours(cycle + 1); }
  107. }
  108. }
  109. }
  110.  
  111. action SetHasInitializedNeighbours(int nextCycle)
  112. {
  113. hasInitializedNeighbours <- true;
  114. nextDrawCycle <- nextCycle;
  115. }
  116.  
  117.  
  118. action Start
  119. {
  120. weight <- 0;
  121. initialized <- true;
  122. hasInitializedNeighbours <- true;
  123.  
  124. loop i from: 0 to: length (neighbours)
  125. {
  126. if (neighbours[i] != nil)
  127. {
  128. ask(neighbours[i]) { do SetHasInitializedNeighbours(1); }
  129. }
  130.  
  131. //ask neighbours[i] {do SetInitialized;}
  132. }
  133. /*
  134. if(!initialized)
  135. {
  136. initialized <- true;
  137. weight <- newWeight + 1;
  138.  
  139. if(weight > maxDistance)
  140. {
  141. maxDistance <- weight;
  142. }
  143.  
  144. loop i from: 0 to: length (neighbours)
  145. {
  146. ask neighbours[i] {do Start(weight);}
  147. }
  148. }
  149. */
  150. }
  151.  
  152.  
  153. reflex draw
  154. {
  155. if(maxDistance = 0)
  156. {
  157. color <- rgb(255, 255, 255);
  158. return;
  159. }
  160.  
  161. int redValue <- 255 * (weight / maxDistance);
  162. int greenValue <- 255 * (1 - (weight / maxDistance));
  163.  
  164. color <- rgb(redValue, greenValue, 0);
  165. }
  166.  
  167. reflex set_weights_to_neighbours when: initialized {
  168. }
  169.  
  170. aspect base {
  171. draw circle(2) color: color ;
  172. }
  173. }
  174.  
  175. species Target {
  176. float size <- 1.0 ;
  177. bool started <- false;
  178.  
  179. rgb color <- #black ;
  180. pos_cell myCell <- one_of (pos_cell);
  181.  
  182. init
  183. {
  184. location <- myCell.location;
  185. }
  186.  
  187. aspect base
  188. {
  189. draw circle(size) color: color ;
  190. }
  191.  
  192. reflex initializeRarget when: !started
  193. {
  194. ask(myCell) { do Start; }
  195. started <- true;
  196. }
  197. }
  198.  
  199. experiment path_finding type: gui {
  200. output {
  201. display main_display {
  202. grid pos_cell lines: #black ;
  203. species Target aspect: base ;
  204. // species actor aspect: base ;
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement