-
Notifications
You must be signed in to change notification settings - Fork 4
/
adult-gangster.nls
406 lines (342 loc) · 10.8 KB
/
adult-gangster.nls
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
adult-gangsters-own [
role
age
status
money
hometerritory
home-id
next-task
stoppedByPolice?
stoppedByAdult?
isCarryingDrugs
gangster-hideout
homexy array
police-ID
done-negotiating?
numberOfConvictions
gangMembers
gang
arrested-time
turn-to-negotiate
inPrison?
drugs-sold
adult-current-customer
pickup-place
drugOrders
isWorking?
adult-gangster-drugs-delivered?
;Maybe change name later, increase when delivered drugs, decrease when gangster goes to jail?
status
leader?
;Distrust against gang leader
distrust
night-time
]
; the set up needs to implement the new terretories,
to setup-adult-Gangster
let Q number-of-adultGangster
ask n-of Q lowerclass
[sprout-adult-gangsters 1 [
set size 2
set shape "person"
ifelse random 100 < 49 [set gang red set color red + 2]
[
set gang blue set color blue + 2
]
set next-task [ -> adult-gangster-idle]]
sprout-gangster-homes 1 [set shape "house" set color brown ]
]
ask n-of Q middleclass
[sprout-adult-gangsters 1 [
set size 2
set shape "person"
ifelse random 100 < 49 [set gang red set color red + 2]
[
set gang blue set color blue + 2
]
set next-task [ -> adult-gangster-idle]]
sprout-gangster-homes 1 [set shape "house" set color brown]
]
ask n-of Q upperclass
[sprout-adult-gangsters 1 [
set size 2
set shape "person"
ifelse random 100 < 49 [set gang red set color red + 2]
[
set gang blue set color blue + 2
]
set next-task [ -> adult-gangster-idle]
]sprout-gangster-homes 1 [ set shape "house" set color brown] ]
ask adult-gangsters [
set homexy array:from-list n-values 2 [0]
array:set homexy 0 xcor array:set homexy 1 ycor
set gangster-hideout min-one-of hideouts [distance myself]
ifelse gang = red
[setupGangmembers red]
[setupGangmembers blue]
set isWorking? false
set drugOrders []
set numberOfConvictions 0
set stoppedByPolice? false
set arrested-time -1
set night-time 0
set inPrison? false
set drugs-sold 0
;print self print gangMembers
]
ask one-of adult-gangsters with [gang = red] [
set leader? true
set size 3
]
ask one-of adult-gangsters with [gang = blue] [
set leader? true
set size 3
]
end
;to setup-hidingplaces
;
; create-hidingplaces 2 [
; set shape "house"
; set color green
; set size 2
; setxy random-xcor random-ycor
; set next-task ""
; ]
;
;end
; in the idle state the gangsters priorites the states
; after each task/state/tick the gangster will come back to the idle state
; to see if they should continue doing what they are doing or if something else with higher priority
; has been "activated"
;
to adult-gangster-idle
;Dummy code to test distrust variable, should be called when a member in the gang gets put in prison
if random 50 = 0 and distrust < count adult-gangsters with [gang = gang] [
set distrust distrust + 1
]
;Starts a vote for a new leader if the distrust for a gangster is higher than the gang member count
if (distrust >= count adult-gangsters with [gang = gang]) [
start-vote gang
set distrust distrust - 10
]
ifelse stoppedByPolice? = true [set next-task [ -> adult-gangster-stand]][ ; highest priority is to stand still if STOPPED
;set hidden? false ; for demo purpose
let policeClose police in-cone 4 35 ; see if there is police in the gangsters line of sight
ifelse any? policeClose [set next-task [ -> adult-gangster-hide]] [ ; police is in line of sight hide
set next-task [-> adult-gangster-work]
]
]
if (time >= 950) or (time <= 299) [
if (night-time = 0 ) [
let rand random 4
ifelse( rand = 0) [
set night-time 1
set next-task [ -> adult-gangster-idle]][
set night-time 1
set next-task [ -> adult-gangster-go-home]
]
]
]
if ((time >= 300) and (time <= 949)) and (night-time = 1 ) [
set night-time 0
]
; <------------------------------------------ astronomical-day-state = "day" doesnt work old code ---- ifelse astronomical-day-state = "day" [set next-task [-> adult-gangster-work]
end
; the gangsters does not have a home right now
; so during the night they are just standing still
;
to adult-gangster-go-home
if (distancexy array:item homexy 0 array:item homexy 1 > 1)
[set heading towardsxy array:item homexy 0 array:item homexy 1
forward 0.25
]
ifelse (time = 300) [
set next-task [ -> adult-gangster-idle]
set night-time 0][
set next-task [ -> adult-gangster-go-home]]
end
; when the police orders the gangster to stand still they come to this state and stand still
; the stoppedByPolice should be toggled by the police when they are finished searching the gangster
;
to adult-gangster-stand
if [ search-tick ] of police-ID >= 10 [
if done-negotiating? = false [
if turn-to-negotiate = 1 [
set offered-money money / 2
set turn-to-negotiate turn-to-negotiate + 1
]
if turn-to-negotiate = 3 [
set offered-money money
set turn-to-negotiate turn-to-negotiate + 1
]
]
]
if arrested-time != -1 [
set done-negotiating? false
set next-task [ -> adult-gangsters-go-to-prison ]
set inPrison? true
]
if stoppedByPolice? = false [
set next-task [ -> adult-gangster-go-home ]
]
end
; if there is police in the gangsters line of sight we come to this state
; the gangster will walk to the closest hiding place, when finished
;
to adult-gangster-hide
; lt 180 ; turn 180 degrees and walk that way
; set hidden? true ; for demo purpose
face gangster-hideout
forward 0.25
ifelse ticks mod 50 = 0 and distance gangster-hideout < 0.3 [
set next-task [ -> adult-gangster-idle]
][
set next-task [ -> adult-gangster-hide]
]
end
; here we will implement another statemachine for working with actions such as
; take drug orders
; tell child-gangsters to deliver orders
; recruit adults to join gang
to adult-gangster-work
set isWorking? true
ifelse empty? drugOrders = false
[
set next-task [-> drug-order]
][
right random 45
left random 45
forward 0.5
set next-task [ -> adult-gangster-idle]
]
if adult-gangster-drugs-delivered? = true [
set status status + 5 + random 10
set label status
set next-task [ -> confirm-delivery pickup-place]
]
end
to confirm-delivery [place-to-pickup]
ask adult-current-customer[
set pickUpPlace place-to-pickup
set drugsDelivered? true
]
set drugs-sold drugs-sold + 1
set adult-gangster-drugs-delivered? false
set next-task[-> adult-gangster-idle]
end
to drug-order
let closestHideout min-one-of other hideouts [distance myself]
let childGangsterClose min-one-of other child-gangsters [distance myself]
set adult-current-customer first drugOrders
let gangster-order self
if childGangsterClose != nobody and [slinging?] of childGangsterClose = false and [hidden?] of childGangsterClose = false [
set drugOrders remove-item 0 drugOrders
ask childGangsterClose [
set hideoutAdress closestHideout
set adult-gangster-order gangster-order
set slinging? true
set next-task [-> recieve-delivery-instructions] ; Ersätt med att ge delivery instructions
]
]
;set drugOrders remove-item 0 drugOrders ;-- this should remove the order only when the drugs are delivered.
;show drugOrders
set size 1
set stoppedByAdult? false
set next-task [-> adult-gangster-idle]
end
;Kick the leader? Not a state
to start-vote [gang-color]
let totalDistrust 0
let gc gang-color
let gang-members count adult-gangsters with [gang = gc]
;Get the total distrust of all the gang members
ask adult-gangsters with [gang = gc] [
set totalDistrust totalDistrust + distrust
]
; Assign a new leader if the majority of the gang has a high distrust rate
if (totalDistrust >= (gang-members ^ 2) / 2) [
ask adult-gangsters with [gang = gc and leader? = true] [
set leader? false
set size 2
set status 0
assign-leader gc
]
]
end
; Assign the gangster with the highest status as the new gang leader
to assign-leader [gang-color]
let gc gang-color
let max-status 0
let new-leader 0
; Reset distrust for all gang members and get member with highest status
ask adult-gangsters with [gang = gc] [
if status >= max-status [
set max-status status
set new-leader self
set distrust 0
]
]
; Assign new leader
ask new-leader [
set leader? true
set size 3
]
end
to adult-gangsters-go-to-prison
ifelse distance min-one-of prison [ distance myself ] > 1 [
set heading towards-nowrap one-of prison with [ count turtles-here = 0 ]
forward 0.5
] [
forward 2
set inPrison? true
set next-task [ -> adult-gangsters-in-prison ]
]
end
to adult-gangsters-in-prison
ifelse ticks - arrested-time = 250 * (numberOfConvictions + 2 * drugs-sold) [
set arrested-time -1
set color gang + 2
set stoppedByPolice? false
set inPrison? false
set next-task [ -> adult-gangster-go-home ]
] [
ifelse [ pcolor ] of patch-ahead 0.5 = grey [
right random 45
left random 45
forward 0.5
] [
right 180
forward 0.5
]
if any? adults in-cone 1 360 with [inPrison? = true] [
let adult-to-recruit one-of adults in-cone 1 360
set next-task [ -> adult-gangsters-prison-recruit adult-to-recruit ]
]
]
end
to adult-gangsters-prison-recruit [to-recruit]
if [numberOfConvictions] of to-recruit > 1 or [homeArea] of to-recruit = 1 and [money] of to-recruit < 100 [
if random 3 = 0[ ;trist
let adult-recruiter who
let storeTime [arrested-time] of to-recruit
ask to-recruit [
set breed adult-gangsters
set shape "person"
set inPrison? true
set numberOfConvictions [numberOfConvictions] of to-recruit
set arrested-time storeTime
set gang [gang] of adult-gangster adult-recruiter
set color gang + 2
set homexy array:from-list n-values 2 [0]
array:set homexy 0 xcor array:set homexy 1 ycor
set gangster-hideout min-one-of hideouts [distance myself]
setupGangmembers gang
set isWorking? false
set drugOrders []
set stoppedByPolice? false
set next-task [ -> adult-gangsters-in-prison ]
]
]
]
set next-task [ -> adult-gangsters-in-prison ]
end