Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- * Name: PathFinding
- * Author: pavel
- * Description:
- * Tags: Tag1, Tag2, TagN
- ***/
- model PathFinding
- global {
- int size <- 30;
- int objects_count <- 3;
- int maxDistance <- 0;
- init {
- // create actor number: objects_count;
- create Target number: objects_count;
- }
- }
- /*
- species actor {
- float size <- 1.0 ;
- int x;
- int y;
- rgb color <- #blue ;
- pos_cell myCell <- one_of (pos_cell);
- init {
- location <- myCell.location;
- }
- string text;
- reflex Move {
- list<pos_cell> neighbs <- (myCell neighbors_at 1);
- int minDist <- 60;
- int minDistIndex <- 0;
- loop k from: 0 to: length(neighbs)-1 {
- if (neighbs[k].weight < minDist)
- {
- minDist <- neighbs[k].weight;
- minDistIndex <- k;
- }
- }
- text <- neighbs[minDistIndex].weight;
- myCell <- neighbs[minDistIndex];
- location <- myCell.location;
- }
- aspect base {
- draw circle(size) color: color ;
- draw 'Actor ' + text color: #black;
- }
- }
- */
- grid pos_cell width: size height: size neighbors: 4 {
- list<pos_cell> neighbours <- (self neighbors_at 1);
- rgb color <- rgb(255,255,255);
- int weight <- 0;
- bool initialized <- false;
- bool hasInitializedNeighbours <- false;
- int nextDrawCycle <- 9999999;
- reflex Initialize when: ((!initialized) and (hasInitializedNeighbours)) and (cycle >= nextDrawCycle)
- {
- int minWeight <- 99999;
- loop i from: 0 to: length (neighbours)
- {
- if(neighbours[i] != nil)
- {
- if(neighbours[i].initialized)
- {
- write("Initialized neigh weight" + neighbours[i].weight);
- if(neighbours[i].weight < minWeight)
- {
- minWeight <- neighbours[i].weight;
- }
- }
- }
- }
- weight <- minWeight + 1;
- if(weight > maxDistance)
- {
- maxDistance <- weight;
- }
- initialized <- true;
- loop i from: 0 to: length (neighbours)
- {
- if(neighbours[i] != nil)
- {
- ask(neighbours[i]) { do SetHasInitializedNeighbours(cycle + 1); }
- }
- }
- }
- action SetHasInitializedNeighbours(int nextCycle)
- {
- hasInitializedNeighbours <- true;
- nextDrawCycle <- nextCycle;
- }
- action Start
- {
- weight <- 0;
- initialized <- true;
- hasInitializedNeighbours <- true;
- loop i from: 0 to: length (neighbours)
- {
- if (neighbours[i] != nil)
- {
- ask(neighbours[i]) { do SetHasInitializedNeighbours(1); }
- }
- //ask neighbours[i] {do SetInitialized;}
- }
- /*
- if(!initialized)
- {
- initialized <- true;
- weight <- newWeight + 1;
- if(weight > maxDistance)
- {
- maxDistance <- weight;
- }
- loop i from: 0 to: length (neighbours)
- {
- ask neighbours[i] {do Start(weight);}
- }
- }
- */
- }
- reflex draw
- {
- if(maxDistance = 0)
- {
- color <- rgb(255, 255, 255);
- return;
- }
- int redValue <- 255 * (weight / maxDistance);
- int greenValue <- 255 * (1 - (weight / maxDistance));
- color <- rgb(redValue, greenValue, 0);
- }
- reflex set_weights_to_neighbours when: initialized {
- }
- aspect base {
- draw circle(2) color: color ;
- }
- }
- species Target {
- float size <- 1.0 ;
- bool started <- false;
- rgb color <- #black ;
- pos_cell myCell <- one_of (pos_cell);
- init
- {
- location <- myCell.location;
- }
- aspect base
- {
- draw circle(size) color: color ;
- }
- reflex initializeRarget when: !started
- {
- ask(myCell) { do Start; }
- started <- true;
- }
- }
- experiment path_finding type: gui {
- output {
- display main_display {
- grid pos_cell lines: #black ;
- species Target aspect: base ;
- // species actor aspect: base ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement