-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWARM2020
145 lines (95 loc) · 2.39 KB
/
SWARM2020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
; Mine Detecting Swarming Robots!
; (c) Dr. Timothy Barker, (2013-2020)
; mostly uses a load of global variables to keep track of events etc. (not great practice but it works!)
globals [dead-turtle-timer dead-turtle-x dead-turtle-y mine-present i num-turtles friend1 friend2 friend1-neighbors friend2-neighbors number-mines]
; deloyment of swarm bots each having very basic behaviour but with interesting emergent dynamics?
to deploy
clear-turtles
; TOTAL number of bots!!!
set num-turtles 16
create-turtles num-turtles
reset-ticks
random-seed new-seed
;; deploy bots
spread
end
to spread
set i 0
ask turtles [
;; orient bots radially in one circle
set heading (i / num-turtles) * 360
set i i + 1
;; deploy bots and move forward
if (i <= num-turtles) [
fd 6 ]
]
tick
end
; friend with dying bot
to friend
set friend1 self
set friend2 0
repeat num-turtles [
;; as long as linking conditions are met (no dead bot and not linked to self) keep linking
linkup
set friend2 friend2 + 1
]
die
end
; turn all bots (upon mine detection)
to linkup
; if not linking to self and both bots exist
if not (friend1 = turtle friend2) and not (friend1 = nobody) and not (turtle friend2 = nobody) [
; turtle turns towards dying one
ask turtle friend2 [
facexy dead-turtle-x dead-turtle-y]
]
end
; send those bots on their way!
to march
ask turtles [
if check-mine [
; all bots are friended with a dying bot
friend
]
; creep forward
fd 1
]
tick
end
;; bring all bots back to base
to retreat
ask turtles [
facexy 0 0
fd 1
]
tick
end
;; set up random mine locations (red squares) CLUSTERS!
to mine
clear-all
let targetminex random-pxcor
let targetminey random-pycor
;; NUMBER of mines!!!
repeat 30 [
let targetminex2 targetminex + random 30
let targetminey2 targetminey + random 30
if (abs (targetminex2) < 32) and (abs (targetminey2) < 32) [
ask patch targetminex2 targetminey2 [
set pcolor red
]]]
end
;; report true if the square at the current bot is red ie. its on an mine!!!
to-report check-mine
if not (pcolor = red) [
report false
]
if pcolor = red [
set dead-turtle-x xcor
set dead-turtle-y ycor
repeat 30 [
set pcolor white
set pcolor black ]
report true
]
end