-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path_ai_controller_blackboard.dm
315 lines (282 loc) · 11.1 KB
/
_ai_controller_blackboard.dm
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
/// Returns true if we have a blackboard key with the provided key and it is not qdeleting
/datum/ai_controller/proc/blackboard_key_exists(key)
var/datum/key_value = blackboard[key]
if (isdatum(key_value))
return !QDELETED(key_value)
if (islist(key_value))
return length(key_value) > 0
return !!key_value
/**
* Used to manage references to datum by AI controllers
*
* * tracked_datum - something being added to an ai blackboard
* * key - the associated key
*/
#define TRACK_AI_DATUM_TARGET(tracked_datum, key) do { \
if(isweakref(tracked_datum)) { \
var/datum/weakref/_bad_weakref = tracked_datum; \
stack_trace("Weakref (Actual datum: [_bad_weakref.resolve()]) found in ai datum blackboard! \
This is an outdated method of ai reference handling, please remove it."); \
}; \
else if(isdatum(tracked_datum)) { \
var/datum/_tracked_datum = tracked_datum; \
if(!HAS_TRAIT_FROM(_tracked_datum, TRAIT_AI_TRACKING, "[REF(src)]_[key]")) { \
RegisterSignal(_tracked_datum, COMSIG_PARENT_QDELETING, PROC_REF(sig_remove_from_blackboard), override = TRUE); \
ADD_TRAIT(_tracked_datum, TRAIT_AI_TRACKING, "[REF(src)]_[key]"); \
}; \
}; \
} while(FALSE)
/**
* Used to clear previously set reference handing by AI controllers
*
* * tracked_datum - something being removed from an ai blackboard
* * key - the associated key
*/
#define CLEAR_AI_DATUM_TARGET(tracked_datum, key) do { \
if(isdatum(tracked_datum)) { \
var/datum/_tracked_datum = tracked_datum; \
REMOVE_TRAIT(_tracked_datum, TRAIT_AI_TRACKING, "[REF(src)]_[key]"); \
if(!HAS_TRAIT(_tracked_datum, TRAIT_AI_TRACKING)) { \
UnregisterSignal(_tracked_datum, COMSIG_PARENT_QDELETING); \
}; \
}; \
} while(FALSE)
/// Used for above to track all the keys that have registered a signal
#define TRAIT_AI_TRACKING "tracked_by_ai"
/**
* Sets the key to the passed "thing".
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/set_blackboard_key(key, thing)
// Assume it is an error when trying to set a value overtop a list
if(islist(blackboard[key]))
CRASH("set_blackboard_key attempting to set a blackboard value to key [key] when it's a list!")
// Don't do anything if it's already got this value
if (blackboard[key] == thing)
return
// Clear existing values
if(!isnull(blackboard[key]))
clear_blackboard_key(key)
TRACK_AI_DATUM_TARGET(thing, key)
blackboard[key] = thing
post_blackboard_key_set(key)
/**
* Helper to force a key to be a certain thing no matter what's already there
*
* Useful for if you're overriding a list with a new list entirely,
* as otherwise it would throw a runtime error from trying to override a list
*
* Not necessary to use if you aren't dealing with lists, as set_blackboard_key will clear the existing value
* in that case already, but may be useful for clarity.
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/override_blackboard_key(key, thing)
if(blackboard[key] == thing)
return
clear_blackboard_key(key)
set_blackboard_key(key, thing)
/**
* Sets the key at index thing to the passed value
*
* Assumes the key value is already a list, if not throws an error.
*
* * key - A blackboard key, with its value set to a list
* * thing - a value which becomes the inner list value's key
* * value - what to set the inner list's value to
*/
/datum/ai_controller/proc/set_blackboard_key_assoc(key, thing, value)
if(!islist(blackboard[key]))
CRASH("set_blackboard_key_assoc called on non-list key [key]!")
// Don't do anything if it's already got this value
if (blackboard[key][thing] == value)
return
TRACK_AI_DATUM_TARGET(thing, key)
TRACK_AI_DATUM_TARGET(value, key)
blackboard[key][thing] = value
post_blackboard_key_set(key)
/**
* Similar to [proc/set_blackboard_key_assoc] but operates under the assumption the key is a lazylist (so it will create a list)
* More dangerous / easier to override values, only use when you want to use a lazylist
*
* * key - A blackboard key, with its value set to a list
* * thing - a value which becomes the inner list value's key
* * value - what to set the inner list's value to
*/
/datum/ai_controller/proc/set_blackboard_key_assoc_lazylist(key, thing, value)
LAZYINITLIST(blackboard[key])
// Don't do anything if it's already got this value
if (blackboard[key][thing] == value)
return
TRACK_AI_DATUM_TARGET(thing, key)
TRACK_AI_DATUM_TARGET(value, key)
blackboard[key][thing] = value
post_blackboard_key_set(key)
/**
* Called after we set a blackboard key, forwards signal information.
*/
/datum/ai_controller/proc/post_blackboard_key_set(key)
if (isnull(pawn))
return
SEND_SIGNAL(pawn, COMSIG_AI_BLACKBOARD_KEY_SET(key), key)
/**
* Adds the passed "thing" to the associated key
*
* Works with lists or numbers, but not lazylists.
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/add_blackboard_key(key, thing)
TRACK_AI_DATUM_TARGET(thing, key)
blackboard[key] += thing
/**
* Similar to [proc/add_blackboard_key], but performs an insertion rather than an add
* Throws an error if the key is not a list already, intended only for use with lists
*
* * key - A blackboard key, with its value set to a list
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/insert_blackboard_key(key, thing)
if(!islist(blackboard[key]))
CRASH("insert_blackboard_key called on non-list key [key]!")
TRACK_AI_DATUM_TARGET(thing, key)
blackboard[key] |= thing
/**
* Adds the passed "thing" to the associated key, assuming key is intended to be a lazylist (so it will create a list)
* More dangerous / easier to override values, only use when you want to use a lazylist
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/add_blackboard_key_lazylist(key, thing)
LAZYINITLIST(blackboard[key])
TRACK_AI_DATUM_TARGET(thing, key)
blackboard[key] += thing
/**
* Similar to [proc/insert_blackboard_key_lazylist], but performs an insertion / or rather than an add
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/insert_blackboard_key_lazylist(key, thing)
LAZYINITLIST(blackboard[key])
TRACK_AI_DATUM_TARGET(thing, key)
blackboard[key] |= thing
/**
* Adds the value to the inner list at key with the inner key set to "thing"
* Throws an error if the key is not a list already, intended only for use with lists
*
* * key - A blackboard key, with its value set to a list
* * thing - a value which becomes the inner list value's key
* * value - what to set the inner list's value to
*/
/datum/ai_controller/proc/add_blackboard_key_assoc(key, thing, value)
if(!islist(blackboard[key]))
CRASH("add_blackboard_key_assoc called on non-list key [key]!")
TRACK_AI_DATUM_TARGET(thing, key)
TRACK_AI_DATUM_TARGET(value, key)
blackboard[key][thing] += value
/**
* Similar to [proc/add_blackboard_key_assoc], assuming key is intended to be a lazylist (so it will create a list)
* More dangerous / easier to override values, only use when you want to use a lazylist
*
* * key - A blackboard key, with its value set to a list
* * thing - a value which becomes the inner list value's key
* * value - what to set the inner list's value to
*/
/datum/ai_controller/proc/add_blackboard_key_assoc_lazylist(key, thing, value)
LAZYINITLIST(blackboard[key])
TRACK_AI_DATUM_TARGET(thing, key)
TRACK_AI_DATUM_TARGET(value, key)
blackboard[key][thing] += value
/**
* Clears the passed key, resetting it to null
*
* Not intended for use with list keys - use [proc/remove_thing_from_blackboard_key] if you are removing a value from a list at a key
*
* * key - A blackboard key
*/
/datum/ai_controller/proc/clear_blackboard_key(key)
if(isnull(blackboard[key]))
return
if(pawn && (SEND_SIGNAL(pawn, COMSIG_AI_BLACKBOARD_KEY_PRECLEAR(key))))
return
CLEAR_AI_DATUM_TARGET(blackboard[key], key)
blackboard[key] = null
if(isnull(pawn))
return
SEND_SIGNAL(pawn, COMSIG_AI_BLACKBOARD_KEY_CLEARED(key))
/**
* Remove the passed thing from the associated blackboard key
*
* Intended for use with lists, if you're just clearing a reference from a key use [proc/clear_blackboard_key]
*
* * key - A blackboard key
* * thing - a value to set the blackboard key to.
*/
/datum/ai_controller/proc/remove_thing_from_blackboard_key(key, thing)
var/associated_value = blackboard[key]
if(thing == associated_value)
stack_trace("remove_thing_from_blackboard_key was called un-necessarily in a situation where clear_blackboard_key would suffice. ")
clear_blackboard_key(key)
return
if(!islist(associated_value))
CRASH("remove_thing_from_blackboard_key called with an invalid \"thing\" argument ([thing]). \
(The associated value of the passed key is not a list and is also not the passed thing, meaning it is clearing an unintended value.)")
for(var/inner_key in associated_value)
if(inner_key == thing)
// flat list
CLEAR_AI_DATUM_TARGET(thing, key)
associated_value -= thing
return
else if(associated_value[inner_key] == thing)
// assoc list
CLEAR_AI_DATUM_TARGET(thing, key)
associated_value -= inner_key
return
CRASH("remove_thing_from_blackboard_key called with an invalid \"thing\" argument ([thing]). \
(The passed value is not tracked in the passed list.)")
///removes a tracked object from a lazylist
/datum/ai_controller/proc/remove_from_blackboard_lazylist_key(key, thing)
var/lazylist = blackboard[key]
if(isnull(lazylist))
return
for(var/key_index in lazylist)
if(thing == key_index || lazylist[key_index] == thing)
CLEAR_AI_DATUM_TARGET(thing, key)
lazylist -= key_index
break
if(!LAZYLEN(lazylist))
clear_blackboard_key(key)
/// Signal proc to go through every key and remove the datum from all keys it finds
/datum/ai_controller/proc/sig_remove_from_blackboard(datum/source)
SIGNAL_HANDLER
var/list/list/remove_queue = list(blackboard)
var/index = 1
while(index <= length(remove_queue))
var/list/next_to_clear = remove_queue[index]
for(var/inner_value in next_to_clear)
var/associated_value = next_to_clear[inner_value]
// We are a lists of lists, add the next value to the queue so we can handle references in there
// (But we only need to bother checking the list if it's not empty.)
if(islist(inner_value) && length(inner_value))
UNTYPED_LIST_ADD(remove_queue, inner_value)
// We found the value that's been deleted. Clear it out from this list
else if(inner_value == source)
next_to_clear -= inner_value
// We are an assoc lists of lists, the list at the next value so we can handle references in there
// (But again, we only need to bother checking the list if it's not empty.)
if(islist(associated_value) && length(associated_value))
UNTYPED_LIST_ADD(remove_queue, associated_value)
// We found the value that's been deleted, it was an assoc value. Clear it out entirely
else if(associated_value == source)
next_to_clear -= inner_value
SEND_SIGNAL(pawn, COMSIG_AI_BLACKBOARD_KEY_CLEARED(inner_value))
index += 1
#undef TRACK_AI_DATUM_TARGET
#undef CLEAR_AI_DATUM_TARGET
#undef TRAIT_AI_TRACKING