Пути в netlo go - как проверить, является ли путь сюда препятствием - PullRequest
0 голосов
/ 26 января 2020

У меня есть следующий код, но он не работает. В проверке процедуры я хотел бы проверить, есть ли в этом пункте (patch-here) «путь», который я построил ранее. Если есть способ, я бы хотел установить коричневый цвет пергамента, если нет, то цвет патча должен быть зеленым. В следующей процедуре я хотел бы проверить цвет и использовать процедуру «стать более популярной» или «стать более популярной» (зависит от цвета). Любые идеи, что я должен изменить в моей процедуре проверки, чтобы она работала правильно?

breed [ buildings building ]

breed [ walkers walker ]
walkers-own [ goal ]

patches-own [ popularity]


globals [ mouse-clicked?
detect-obstacle?]

breed [ obstacles obstacle ]
breed [ trees tree ]
breed [ ways way ]

to setup
  clear-all
  set-default-shape buildings "house"
  set-default-shape trees "tree"
  set-default-shape obstacles "cactus"
  set-default-shape ways "tile brick"
  set-default-shape turtles "turtle"
  ask patches [ set pcolor green ]
  ;;ask patch -42 -28 [set pcolor black]

  create-walkers walker-count [
    setxy random-xcor random-ycor
    set goal one-of patches
    set color yellow
    set size 2
  ]
  create-obstacles 1 [set color blue set size 13 setxy 0 5]
  create-ways 1 [set color black set size 3 setxy -39 -28]
  create-ways 1 [set color black set size 3 setxy -38 -28]
  create-ways 1 [set color black set size 3 setxy -36 -28]
  create-ways 1 [set color black set size 3 setxy -34 -28]
  create-ways 1 [set color black set size 3 setxy -32 -28]
  create-ways 1 [set color black set size 3 setxy -30 -28]
  create-ways 1 [set color black set size 3 setxy -28 -28]
  create-ways 1 [set color black set size 3 setxy -26 -28]
  create-ways 1 [set color black set size 3 setxy -24 -28]
  create-ways 1 [set color black set size 3 setxy -22 -28]

  reset-ticks
end

;; Click to place buildings
;; Have stuff unbecome path once it decays below a certain popularity threshold
to go
  check-building-placement
  move-walkers
  decay-popularity
  recolor-patches

  tick
end



to check-building-placement
  ask patch  0 0 [toggle-building]
end

to toggle-building
  let nearby-buildings buildings in-radius 4
  ifelse any? nearby-buildings [
    ; if there is a building near where the mouse was clicked
    ; (and there should always only be one), we remove it and
    ask nearby-buildings [ die ]
  ] [
    ; if there was no buildings near where
    ; the mouse was clicked, we create one
    sprout-buildings 1 [
      set color cyan
      set size 8
      setxy 25 15
    ]
    sprout-buildings 1[
      set color red
      set size 8
      setxy 44 -34]
    sprout-buildings 1[
      set color yellow
      set size 8
      setxy 0 -28]
    sprout-buildings 1[
      set color pink
      set size 8
      setxy -43 -28]
    sprout-buildings 1[
      set color blue
      set size 8
      setxy -20 5]
    sprout-buildings 1[
      set color violet
      set size 8
      setxy 10 46]
  ]
end

to decay-popularity
  ask patches with [ not any? walkers-here ] [
    set popularity popularity * (100 - popularity-decay-rate) / 100
    ; when popularity is below 1, the patch becomes (or stays) grass
    if popularity < 1 [ set pcolor green ]
  ]
end

to become-more-popular
  set popularity popularity + popularity-per-step
  ; if the increase in popularity takes us above the threshold, become a route
  if popularity >= minimum-route-popularity [ set pcolor gray ]

end


to become-much-more-popular
  set popularity popularity + popularity-per-step + popularity + popularity-per-step
  ; if the increase in popularity takes us above the threshold, become a route
  if popularity >= minimum-route-popularity [ set pcolor gray ]

end

to move-walkers
  ask walkers [
    ifelse patch-here = goal [
      ifelse count buildings >= 2 [
        set goal [ patch-here ] of one-of buildings
      ] [
        set goal one-of patches
      ]
    ] [
      walk-towards-goal
    ]
  ]
end

to check 
  ask patch-here [
    if is-ways? = true [set color brown]
  ]
end

;;to walk-towards-goal
 ;; if pcolor != gray [
    ; boost the popularity of the patch we're on
  ;;  ask patch-here [ become-more-popular ]
 ;; ]
 ;; face best-way-to goal
 ;; fd 1
;;end

to walk-towards-goal
  if pcolor = brown [
    ask patch-here [become-much-more-popular]
  ]
  if pcolor = green [
    ask patch-here [become-more-popular]
  ]
  face best-way-to goal
  fd 1
end


to-report best-way-to [ destination ]

  ; of all the visible route patches, select the ones
  ; that would take me closer to my destination
  let visible-patches patches in-radius walker-vision-dist
  let visible-routes visible-patches with [ pcolor = gray ]
  let routes-that-take-me-closer visible-routes with [
    distance destination < [ distance destination - 1 ] of myself
  ]

  ifelse any? routes-that-take-me-closer [
    ; from those route patches, choose the one that is the closest to me
    report min-one-of routes-that-take-me-closer [ distance self ]
  ] [
    ; if there are no nearby routes to my destination
    report destination
  ]

end

to recolor-patches
  ifelse show-popularity? [
    let max-value (minimum-route-popularity * 3)
    ask patches with [ pcolor != gray ] [
      set pcolor scale-color green popularity (- max-value) max-value
    ]
  ] [
    ask patches with [ pcolor != gray ] [
      set pcolor green
    ]
  ]
end





; Copyright 2015 Uri Wilensky.
; See Info tab for full copyright and license.


...