forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_officer.dm
463 lines (388 loc) · 17 KB
/
security_officer.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/datum/job/security_officer
title = "Security Officer"
auto_deadmin_role_flags = DEADMIN_POSITION_SECURITY
department_head = list("Head of Security")
faction = FACTION_STATION
total_positions = 5 //Handled in /datum/controller/occupations/proc/setup_officer_positions()
spawn_positions = 5 //Handled in /datum/controller/occupations/proc/setup_officer_positions()
supervisors = "the head of security, and the head of your assigned department (if applicable)"
selection_color = "#ffeeee"
minimal_player_age = 7
exp_requirements = 300
exp_required_type = EXP_TYPE_CREW
exp_granted_type = EXP_TYPE_CREW
outfit = /datum/outfit/job/security
plasmaman_outfit = /datum/outfit/plasmaman/security
paycheck = PAYCHECK_HARD
paycheck_department = ACCOUNT_SEC
mind_traits = list(TRAIT_DONUT_LOVER)
liver_traits = list(TRAIT_LAW_ENFORCEMENT_METABOLISM)
display_order = JOB_DISPLAY_ORDER_SECURITY_OFFICER
bounty_types = CIV_JOB_SEC
departments_list = list(
/datum/job_department/security,
)
family_heirlooms = list(/obj/item/book/manual/wiki/security_space_law, /obj/item/clothing/head/beret/sec)
mail_goodies = list(
/obj/item/food/donut/caramel = 10,
/obj/item/food/donut/matcha = 10,
/obj/item/food/donut/blumpkin = 5,
/obj/item/clothing/mask/whistle = 5,
/obj/item/melee/baton/security/boomerang/loaded = 1
)
rpg_title = "Guard"
job_flags = JOB_ANNOUNCE_ARRIVAL | JOB_CREW_MANIFEST | JOB_EQUIP_RANK | JOB_CREW_MEMBER | JOB_NEW_PLAYER_JOINABLE | JOB_REOPEN_ON_ROUNDSTART_LOSS | JOB_ASSIGN_QUIRKS
GLOBAL_LIST_INIT(available_depts, list(SEC_DEPT_ENGINEERING, SEC_DEPT_MEDICAL, SEC_DEPT_SCIENCE, SEC_DEPT_SUPPLY))
/**
* The department distribution of the security officers.
*
* Keys are refs of the security officer mobs. This is to preserve the list's structure even if the
* mob gets deleted. This is also safe, as mobs are guaranteed to have a unique ref, as per /mob/GenerateTag().
*/
GLOBAL_LIST_EMPTY(security_officer_distribution)
/datum/job/security_officer/after_roundstart_spawn(mob/living/spawning, client/player_client)
. = ..()
if(ishuman(spawning))
setup_department(spawning, player_client)
/datum/job/security_officer/after_latejoin_spawn(mob/living/spawning)
. = ..()
if(ishuman(spawning))
var/department = setup_department(spawning, spawning.client)
if(department)
announce_latejoin(spawning, department, GLOB.security_officer_distribution)
/// Returns the department this mob was assigned to, if any.
/datum/job/security_officer/proc/setup_department(mob/living/carbon/human/spawning, client/player_client)
var/department = player_client?.prefs?.read_preference(/datum/preference/choiced/security_department)
if (!isnull(department))
department = get_my_department(spawning, department)
// This should theoretically still run if a player isn't in the distributions, but isn't a late join.
GLOB.security_officer_distribution[REF(spawning)] = department
var/ears = null
var/accessory = null
var/list/dep_trim = null
var/destination = null
switch(department)
if(SEC_DEPT_SUPPLY)
ears = /obj/item/radio/headset/headset_sec/alt/department/supply
dep_trim = /datum/id_trim/job/security_officer/supply
destination = /area/security/checkpoint/supply
accessory = /obj/item/clothing/accessory/armband/cargo
if(SEC_DEPT_ENGINEERING)
ears = /obj/item/radio/headset/headset_sec/alt/department/engi
dep_trim = /datum/id_trim/job/security_officer/engineering
destination = /area/security/checkpoint/engineering
accessory = /obj/item/clothing/accessory/armband/engine
if(SEC_DEPT_MEDICAL)
ears = /obj/item/radio/headset/headset_sec/alt/department/med
dep_trim = /datum/id_trim/job/security_officer/medical
destination = /area/security/checkpoint/medical
accessory = /obj/item/clothing/accessory/armband/medblue
if(SEC_DEPT_SCIENCE)
ears = /obj/item/radio/headset/headset_sec/alt/department/sci
dep_trim = /datum/id_trim/job/security_officer/science
destination = /area/security/checkpoint/science
accessory = /obj/item/clothing/accessory/armband/science
if(accessory)
var/obj/item/clothing/under/worn_under = spawning.w_uniform
worn_under.attach_accessory(new accessory)
if(ears)
if(spawning.ears)
qdel(spawning.ears)
spawning.equip_to_slot_or_del(new ears(spawning),ITEM_SLOT_EARS)
// If there's a departmental sec trim to apply to the card, overwrite.
if(dep_trim)
var/obj/item/card/id/worn_id = spawning.get_idcard(hand_first = FALSE)
SSid_access.apply_trim_to_card(worn_id, dep_trim)
spawning.sec_hud_set_ID()
var/spawn_point = pick(LAZYACCESS(GLOB.department_security_spawns, department))
if(!CONFIG_GET(flag/sec_start_brig) && (destination || spawn_point))
if(spawn_point)
spawning.Move(get_turf(spawn_point))
else
var/list/possible_turfs = get_area_turfs(destination)
while (length(possible_turfs))
var/random_index = rand(1, length(possible_turfs))
var/turf/target = possible_turfs[random_index]
if (spawning.Move(target))
break
possible_turfs.Cut(random_index, random_index + 1)
if(player_client)
if(department)
to_chat(player_client, "<b>You have been assigned to [department]!</b>")
else
to_chat(player_client, "<b>You have not been assigned to any department. Patrol the halls and help where needed.</b>")
return department
/datum/job/security_officer/proc/announce_latejoin(
mob/officer,
department,
distribution,
)
var/obj/machinery/announcement_system/announcement_system = pick(GLOB.announcement_systems)
if (isnull(announcement_system))
return
announcement_system.announce_officer(officer, department)
var/list/targets = list()
var/list/partners = list()
for (var/officer_ref in distribution)
var/mob/partner = locate(officer_ref)
if (!istype(partner) || distribution[officer_ref] != department)
continue
partners += partner.real_name
if (partners.len)
for (var/obj/item/pda/pda as anything in GLOB.PDAs)
if (pda.owner in partners)
targets += STRINGIFY_PDA_TARGET(pda.owner, pda.ownjob)
if (!targets.len)
return
var/datum/signal/subspace/messaging/pda/signal = new(announcement_system, list(
"name" = "Security Department Update",
"job" = "Automated Announcement System",
"message" = "Officer [officer.real_name] has been assigned to your department, [department].",
"targets" = targets,
"automated" = TRUE,
))
signal.send_to_receivers()
/datum/job/security_officer/proc/get_my_department(mob/character, preferred_department)
var/department = GLOB.security_officer_distribution[REF(character)]
// This passes when they are a round start security officer.
if (department)
return department
return get_new_officer_distribution_from_late_join(
preferred_department,
shuffle(GLOB.available_depts),
GLOB.security_officer_distribution,
)
/datum/outfit/job/security
name = "Security Officer"
jobtype = /datum/job/security_officer
id_trim = /datum/id_trim/job/security_officer
uniform = /obj/item/clothing/under/rank/security/officer
suit = /obj/item/clothing/suit/armor/vest/alt
suit_store = /obj/item/gun/energy/disabler
backpack_contents = list(
/obj/item/melee/baton/security/loaded = 1,
/obj/item/modular_computer/tablet/preset/advanced/security = 1,
)
belt = /obj/item/pda/security
ears = /obj/item/radio/headset/headset_sec/alt
gloves = /obj/item/clothing/gloves/color/black
head = /obj/item/clothing/head/helmet/sec
shoes = /obj/item/clothing/shoes/jackboots
l_pocket = /obj/item/restraints/handcuffs
r_pocket = /obj/item/assembly/flash/handheld
backpack = /obj/item/storage/backpack/security
satchel = /obj/item/storage/backpack/satchel/sec
duffelbag = /obj/item/storage/backpack/duffelbag/sec
box = /obj/item/storage/box/survival/security
chameleon_extras = list(
/obj/item/clothing/glasses/hud/security/sunglasses,
/obj/item/clothing/head/helmet,
/obj/item/gun/energy/disabler,
)
//The helmet is necessary because /obj/item/clothing/head/helmet/sec is overwritten in the chameleon list by the standard helmet, which has the same name and icon state
implants = list(/obj/item/implant/mindshield)
/obj/item/radio/headset/headset_sec/alt/department/Initialize(mapload)
. = ..()
wires = new/datum/wires/radio(src)
secure_radio_connections = new
recalculateChannels()
/obj/item/radio/headset/headset_sec/alt/department/engi
keyslot = new /obj/item/encryptionkey/headset_sec
keyslot2 = new /obj/item/encryptionkey/headset_eng
/obj/item/radio/headset/headset_sec/alt/department/supply
keyslot = new /obj/item/encryptionkey/headset_sec
keyslot2 = new /obj/item/encryptionkey/headset_cargo
/obj/item/radio/headset/headset_sec/alt/department/med
keyslot = new /obj/item/encryptionkey/headset_sec
keyslot2 = new /obj/item/encryptionkey/headset_med
/obj/item/radio/headset/headset_sec/alt/department/sci
keyslot = new /obj/item/encryptionkey/headset_sec
keyslot2 = new /obj/item/encryptionkey/headset_sci
/// Returns the distribution of splitting the given security officers into departments.
/// Return value is an assoc list of candidate => SEC_DEPT_*.
/proc/get_officer_departments(list/preferences, list/departments)
if (!preferences.len)
return list()
/**
* This is a pretty complicated algorithm, but it's one I'm rather proud of.
*
* This is the function that is responsible for taking the list of preferences,
* and spitting out what to put them in.
*
* However, it should, wherever possible, prevent solo departments.
* That means that if there's one medical officer, and one engineering officer,
* that they should be put onto the same department (either medical or engineering).
*
* The first step is to get the "distribution". This describes how many officers
* should be in each department, no matter what they are.
* This is handled in `get_distribution`. Examples of inputs/outputs are:
* get_distribution(1, 4) => [1]
* get_distribution(2, 4) => [2]
* get_distribution(3, 4) => [3] # If this returned [2, 1], then we'd get a loner.
* get_distribution(4, 4) => [2, 2] # We have enough to put into a separate group
*
* Once this distribution is received, the next step is to figure out where to put everyone.
*
* If all members have no preference, just make one an unused department (from the departments argument).
* Then, call ourselves again.
*
* Order the groups from most populated to least.
*
* If the top group has enough officers who actually *want* that department, then we give it to them.
* If there are any leftovers (for example, if 3 officers want medical, but we only want 2), then we
* update those to have no preference instead.
*
* If the top group does NOT have enough officers, then we kill the least popular group by setting
* them all to have no preference.
*
* Anyone in the most popular group will be removed from the list, and the final tally will be updated.
* In the case of not having enough officers, this is a no-op, as there won't be any in the most popular group yet.
*
* If there are any candidates left, then we call the algorithm again, but for everyone who hasn't been selected yet.
* We take the results from that run, and put them in the correct order.
*
* As an example, let's assume we have the following preferences:
* [engineer, medical, medical, medical, medical, cargo]
*
* The distribution of this is [2, 2, 2], meaning there will be 3 departments chosen and they will have 2 each.
* We order from most popular to least popular and get:
* - medical: 4
* - engineer: 1
* - cargo: 1
*
* We need 2 to fill the first group. There are enough medical staff to do it. Thus, we take the first 2 medical staff
* and update the output, making it now: [engineer, medical, medical, ?, ?, cargo].
*
* The remaining two want-to-be-medical officers are now updated to act as no preference. We run the algorithm again.
* This time, are candidates are [engineer, none, none, cargo].
* The distribution of this is [2, 2]. The frequency is:
* - engineer: 1
* - cargo: 1
* - no preference: 2
*
* We need 2 to fill the engineering group, but only have one who wants to do it.
* We have enough no preferences for it, making our result: [engineer, engineer, none, cargo].
* We run the algorithm again, but this time with: [none, cargo].
* Frequency is:
* - cargo: 1
* - no preference: 1
* Enough to fill cargo, etc, and we get [cargo, cargo].
*
* These are all then compounded into one list.
*
* In the case that all are no preference, it will pop the last department, and use that.
* For example, if `departments` is [engi, medical, cargo], and we have the preferences:
* [none, none, none]...
* Then we will just give them all cargo.
*
* One of the most important parts of this algorithm is IT IS DETERMINISTIC.
* That means that this proc is 100% testable.
* Instead, to get random results, the preferences and departments are shuffled
* before the proc is ever called.
*/
preferences = preferences.Copy()
departments = departments.Copy()
var/distribution = get_distribution(preferences.len, departments.len)
var/selection[preferences.len]
var/list/grouped = list()
var/list/biggest_group
var/biggest_preference
var/list/indices = list()
for (var/index in 1 to preferences.len)
indices += index
var/preference = preferences[index]
if (!(preference in grouped))
grouped[preference] = list()
grouped[preference] += index
var/list/preferred_group = grouped[preference]
if (preference != SEC_DEPT_NONE && (isnull(biggest_group) || biggest_group.len < preferred_group.len))
biggest_group = grouped[preference]
biggest_preference = preference
if (isnull(biggest_group))
preferences[1] = pop(departments)
return get_officer_departments(preferences, departments)
if (biggest_group.len >= distribution[1])
for (var/index in 1 to distribution[1])
selection[biggest_group[index]] = biggest_preference
if (biggest_group.len > distribution[1])
for (var/leftover in (distribution[1] + 1) to biggest_group.len)
preferences[leftover] = SEC_DEPT_NONE
else
var/needed = distribution[1] - biggest_group.len
if (LAZYLEN(LAZYACCESS(grouped, SEC_DEPT_NONE)) >= needed)
for (var/candidate_index in biggest_group)
selection[candidate_index] = biggest_preference
for (var/index in 1 to needed)
selection[grouped[SEC_DEPT_NONE][index]] = biggest_preference
else
var/least_popular_index = grouped[grouped.len]
if (least_popular_index == SEC_DEPT_NONE)
least_popular_index = grouped[grouped.len - 1]
var/least_popular = grouped[least_popular_index]
for (var/candidate_index in least_popular)
preferences[candidate_index] = SEC_DEPT_NONE
// Remove all members of the most popular candidate from the list
for (var/chosen in 1 to selection.len)
if (selection[chosen] == biggest_preference)
indices -= chosen
preferences[chosen] = null
list_clear_nulls(preferences)
departments -= biggest_preference
if (grouped.len != 1)
var/list/next_step = get_officer_departments(preferences, departments)
for (var/index in 1 to indices.len)
var/place = indices[index]
selection[place] = next_step[index]
return selection
/proc/get_distribution(candidates, departments)
var/number_of_twos = min(departments, round(candidates / 2))
var/redistribute = candidates - (2 * number_of_twos)
var/distribution[max(1, number_of_twos)]
for (var/index in 1 to number_of_twos)
distribution[index] = 2
for (var/index in 0 to redistribute - 1)
distribution[(index % departments) + 1] += 1
return distribution
/proc/get_new_officer_distribution_from_late_join(
preference,
list/departments,
list/distribution,
)
/**
* For late joiners, we're forced to put them in an alone department at some point.
*
* This is because reusing the round-start algorithm would force existing officers into
* a different department in order to preserve having partners at all times.
*
* This would mean retroactively updating their access as well, which is too much
* of a headache for me to want to bother.
*
* So, here's the method. If any department currently has 1 officer, they are forced into
* that.
*
* Otherwise, the department with the least officers in it is chosen.
* Preference takes priority, meaning that if both medical and engineering have zero officers,
* and the preference is medical, then medical is what will be chosen.
*
* Just like `get_officer_departments`, this function is deterministic.
* Randomness should instead be handled in the shuffling of the `departments` argument.
*/
var/list/amount_in_departments = list()
for (var/department in departments)
amount_in_departments[department] = 0
for (var/officer in distribution)
var/department = distribution[officer]
if (!isnull(department))
amount_in_departments[department] += 1
var/list/lowest_departments = list(departments[1])
var/lowest_amount = INFINITY
for (var/department in amount_in_departments)
var/amount = amount_in_departments[department]
if (amount == 1)
return department
else if (lowest_amount > amount)
lowest_departments = list(department)
lowest_amount = amount
else if (lowest_amount == amount)
lowest_departments += department
return (preference in lowest_departments) ? preference : lowest_departments[1]