Capture the Flag

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 4.1pre5 • Viewed 436 times • Downloaded 15 times • Run 0 times
Download the 'Capture the Flag' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


VERSION

$Id: Capture the Flag.nlogo 40967 2008-09-09 19:44:01Z everreau $

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [ min-grade ]

breed [ students student ]
breed [ flags flag ]
breed [ arrows arrow ]

students-own
[
  user-id   
  step-size
  jail-timer
  turbo-timer
  my-goal
  seen-goal?
  my-arrows
]

arrows-own
[
  my-goal
]

patches-own 
[ 
  goal? 
  elevation
]

to startup
  setup
  hubnet-set-client-interface "COMPUTER" []
  hubnet-reset
end 

to setup
  cp
  cd
  clear-output  
  
  set-default-shape flags "flag"
  set-default-shape arrows "arrow"
  set release-the-hounds? false

  ask flags [ die ]

  ask arrows [ die ]
 
  ask patches [ set goal? false ]
  
  ;; even out the colors in case somebody logged out
  let color-difference count students with [color = red] - count students with [color = blue]
  
  if color-difference > 1
  [
    ask n-of floor (color-difference / 2) turtles with [color = red] [ set color blue ]
  ]
  if color-difference < -1
  [
    ask n-of floor abs (color-difference / 2) turtles with [color = blue] [ set color red ]
  ]

  ask patches [ set elevation random-float 10 ]
  repeat 10 [ diffuse elevation 0.5 ]

  ask patches with [pycor > 0] [ set pcolor scale-color blue elevation 0 40 ]
  ask patches with [pycor < 0] [ set pcolor scale-color red elevation 0 40 ]    

  set min-grade min [ my-min-grade ] of patches
        
  ask one-of patches with [ pycor > max-pxcor - 10 ] 
  [ 
    sprout-flags 1 [ set color blue ht ] 
    set goal? true
    ask students with [color = blue ] [ set my-goal myself ]
  ]
  ask one-of patches with [ pycor < min-pxcor + 10 ] 
  [ 
    sprout-flags 1 [ set color red ht ] 
    set goal? true
    ask students with [color = red ] [ set my-goal myself ]
  ]

  ask students
  [
    hubnet-clear-overrides user-id
    set step-size default-step-size
    set jail-timer 0
    set turbo-timer 0
    move-to one-of patches with [shade-of? pcolor [color] of myself]
    hubnet-send-follow user-id self vision
    hubnet-send-override user-id flags "hidden?" [false]
    hubnet-send-override user-id self "shape" ["default halo"]
    set heading one-of [0 90 180 270]
    set seen-goal? false
    create-arrow self my-goal color    
  ]
end 

to create-arrow [root g c]
  set my-arrows no-turtles
  hatch-arrows 1
  [
    ht
    set my-goal g
    set color c
    move-arrow
    hubnet-send-override [user-id] of myself self "hidden?" [ false ]
    ask root [ set my-arrows (turtle-set my-arrows myself) ]
  ]  
end 

to move-arrow
  move-to myself
  face my-goal
  fd distance my-goal * 9 / world-width
end 

to-report my-min-grade
  report min [ elevation - [elevation] of myself ] of neighbors4
end 

;;
;; Runtime Procedures
;;

to go
  every speed
  [
    listen-clients
    display
    ask students 
    [ 
      set jail-timer jail-timer - 1 
      if jail-timer > 0
      [ hubnet-send-override user-id self "label" [jail-timer] ]
      if jail-timer = 0 
      [ hubnet-clear-override user-id self "label"]
      if turbo-timer > 0
      [ 
        set turbo-timer turbo-timer - step-size 
        if turbo-timer <= 0
        [ 
          set jail-timer round ( turbo-length * turbo-rest-fraction / step-size )
          set step-size default-step-size
        ]
      ]
      
      if jail-timer <= 0
      [ move ]
      ask my-arrows [ move-arrow ]
    ]
    if game-over?
    [ stop ]
  ]
end 

to move
  if release-the-hounds?
  [
    let dist step-size + grade
    let next-patch patch-ahead dist
    if next-patch != nobody
    [
      if not any? link-neighbors and [distance [my-goal] of myself] of next-patch < puppy-guarding-radius  
      [ rt 180 ]
    ]
    fd dist
    tag
    grab-flag
    return-flag-to-base
    send-location
    
    if not seen-goal? 
    [ 
      let other-goal one-of patches in-radius 9 with [goal? and not shade-of? [color] of myself pcolor ]
      if other-goal != nobody
      [
        set seen-goal? true
        create-arrow self other-goal ifelse-value (color = red) [blue][red]
      ]
    ]
  ]
end 

to-report grade
  let p patch-ahead 1
  if p = patch-here
  [ set p patch-ahead 1.5 ]
  if p = nobody
  [ report 0 ]
  report ([elevation] of p - elevation) * (step-size - min-step-size) / min-grade
end 

to-report game-over?  
  let over? false
  ask students with [any? link-neighbors and goal? and shade-of? pcolor color]
  [
    user-message  (word user-id " has won the game for " ifelse-value (color = blue) ["blue"]["red"])
    set over? true
  ]
  report over?
end 


;;
;; HubNet Procedures
;;

to listen-clients
  while [ hubnet-message-waiting? ]
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [ create-new-student ]
    [
      ifelse hubnet-exit-message?
      [ remove-student ]
      [ ask students with [user-id = hubnet-message-source]
        [ execute-command hubnet-message-tag ] 
      ]
    ]
  ]
end 

to create-new-student
  create-students 1
  [
    set seen-goal? false
    set heading one-of [0 90 180 270]
    set user-id hubnet-message-source
    set step-size default-step-size
    set color ifelse-value (count students with [color = red] > count students with [color = blue])[blue][red]
    send-location
    move-to one-of patches with [shade-of? pcolor [color] of myself]
    hubnet-send-follow user-id self vision
    hubnet-send-override user-id flags "hidden?" [ false ]
    hubnet-send-override user-id self "shape" [ "default halo" ]
    set my-goal one-of patches with [goal? and shade-of? pcolor [color] of myself ]
    create-arrow self my-goal color
  ]
end 

to remove-student
  ask students with [user-id = hubnet-message-source]
  [ 
    ask my-arrows [ die ]
    die 
  ]
end 

to execute-command [command]
  if jail-timer > 0 or turbo-timer > 0
  [ stop ]
  if command = "right"
  [ rt 90 stop ]
  if command = "left"
  [ lt 90 stop ]
  if command = "turbo"
  [ 
    set turbo-timer turbo-length 
    set step-size (step-size * turbo-speed)
  ]
end 

to send-location ;; student procedure
  hubnet-send user-id "location" (word "(" pxcor "," pycor ")")
end 

to grab-flag ;; student procedure
  if not any? my-links
  [
    let my-flag one-of flags-here with [color != [color] of myself and not any? link-neighbors] 
    if my-flag != nobody
    [ 
      ask my-flag [ move-to myself ]
      create-link-with my-flag [ hide-link tie ]
      if goal?
      [
        let goal patch-here
        ask students
          [ hubnet-send-override user-id goal "pcolor" [pcolor + 4.5] ]
      ]
    ]
  ]
end 

to return-flag-to-base
    let my-flag one-of flags-here with [color = [color] of myself and not any? link-neighbors] 
    if my-flag != nobody
    [
      ask my-flag [ move-to one-of patches with [goal? and shade-of? [color] of myself pcolor] ] 
    ]
end 

to tag ;; student procedure
  if shade-of? pcolor color
  [
    let opponent one-of students-here with [color != [color] of myself]
    if opponent != nobody
    [
      ask opponent [ go-to-jail ]
      set jail-timer 20
    ]
  ]  
end 

to go-to-jail ;; student procedure
  ask link-neighbors 
  [ 
    move-to one-of patches in-radius (puppy-guarding-radius + 1) with [distance [patch-here] of myself > puppy-guarding-radius]     
  ]
  ask my-links [ die ]
  move-to one-of patches with [pycor = 0]
  set jail-timer 100
  send-location
end 

There are 2 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 9 years ago Capture the Flag Download this version
Uri Wilensky almost 9 years ago Capture the Flag Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.