From fad1bff5d6bafefd35f6e7c5d048bc32eec0c6ef Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Sat, 22 Jan 2022 06:00:53 +0100 Subject: [PATCH] [MIRROR] Refactors AI law announcing [MDB IGNORE] (#10886) * Refactors AI law announcing (#64199) This had some weird list manipulation, and generally just a weird structure. I changed it to hold every law it's announcing to be contained within a list, rather than just have a list of Yeses and Nos. The old functionality also increased the length of this list each time laws were checked, meaning theoretically this could cause high memory usage issues. Anyways... fixed all that. * Refactors AI law announcing Co-authored-by: Seth Scherer --- code/datums/ai_laws.dm | 7 +- code/modules/mob/living/silicon/ai/ai.dm | 4 + code/modules/mob/living/silicon/laws.dm | 18 +- code/modules/mob/living/silicon/pai/pai.dm | 2 + .../modules/mob/living/silicon/robot/robot.dm | 2 + code/modules/mob/living/silicon/silicon.dm | 163 ++++++++++-------- 6 files changed, 118 insertions(+), 78 deletions(-) diff --git a/code/datums/ai_laws.dm b/code/datums/ai_laws.dm index e31c83d8cc9..8bfa8835bae 100644 --- a/code/datums/ai_laws.dm +++ b/code/datums/ai_laws.dm @@ -436,15 +436,16 @@ if(force) zeroth = null zeroth_borg = null - return + return TRUE if(owner?.mind?.special_role) - return + return FALSE if (istype(owner, /mob/living/silicon/ai)) var/mob/living/silicon/ai/A=owner if(A?.deployed_shell?.mind?.special_role) - return + return FALSE zeroth = null zeroth_borg = null + return TRUE /datum/ai_laws/proc/associate(mob/living/silicon/M) if(!owner) diff --git a/code/modules/mob/living/silicon/ai/ai.dm b/code/modules/mob/living/silicon/ai/ai.dm index a6a0481c362..ceb93d99ad5 100644 --- a/code/modules/mob/living/silicon/ai/ai.dm +++ b/code/modules/mob/living/silicon/ai/ai.dm @@ -121,8 +121,12 @@ if(L && istype(L, /datum/ai_laws)) laws = L laws.associate(src) + for (var/law in laws.inherent) + lawcheck += law else make_laws() + for (var/law in laws.inherent) + lawcheck += law if(target_ai.mind) target_ai.mind.transfer_to(src) diff --git a/code/modules/mob/living/silicon/laws.dm b/code/modules/mob/living/silicon/laws.dm index 735b59c1c74..518bb4b7f32 100644 --- a/code/modules/mob/living/silicon/laws.dm +++ b/code/modules/mob/living/silicon/laws.dm @@ -32,31 +32,41 @@ /mob/living/silicon/proc/add_inherent_law(law, announce = TRUE) laws_sanity_check() laws.add_inherent_law(law) + lawcheck += law post_lawchange(announce) /mob/living/silicon/proc/clear_inherent_laws(announce = TRUE) laws_sanity_check() + for (var/law in laws.inherent) + if (law in lawcheck) + lawcheck -= law laws.clear_inherent_laws() post_lawchange(announce) /mob/living/silicon/proc/add_supplied_law(number, law, announce = TRUE) laws_sanity_check() laws.add_supplied_law(number, law) + lawcheck += law post_lawchange(announce) /mob/living/silicon/proc/clear_supplied_laws(announce = TRUE) laws_sanity_check() + for(var/law in laws.supplied) + if (law in lawcheck) + lawcheck -= law laws.clear_supplied_laws() post_lawchange(announce) /mob/living/silicon/proc/add_ion_law(law, announce = TRUE) laws_sanity_check() laws.add_ion_law(law) + ioncheck += law post_lawchange(announce) /mob/living/silicon/proc/add_hacked_law(law, announce = TRUE) laws_sanity_check() laws.add_hacked_law(law) + hackedcheck += law post_lawchange(announce) /mob/living/silicon/proc/replace_random_law(law, groups, announce = TRUE) @@ -72,16 +82,20 @@ /mob/living/silicon/proc/remove_law(number, announce = TRUE) laws_sanity_check() . = laws.remove_law(number) + if (. in lawcheck) + lawcheck -= . post_lawchange(announce) /mob/living/silicon/proc/clear_ion_laws(announce = TRUE) laws_sanity_check() laws.clear_ion_laws() + ioncheck = list() post_lawchange(announce) /mob/living/silicon/proc/clear_hacked_laws(announce = TRUE) laws_sanity_check() laws.clear_hacked_laws() + hackedcheck = list() post_lawchange(announce) /mob/living/silicon/proc/make_laws() @@ -91,5 +105,7 @@ /mob/living/silicon/proc/clear_zeroth_law(force, announce = TRUE) laws_sanity_check() - laws.clear_zeroth_law(force) + var/zeroth = laws.zeroth + if(laws.clear_zeroth_law(force)) + lawcheck -= zeroth post_lawchange(announce) diff --git a/code/modules/mob/living/silicon/pai/pai.dm b/code/modules/mob/living/silicon/pai/pai.dm index 69e02e80d21..43d995bbdc4 100644 --- a/code/modules/mob/living/silicon/pai/pai.dm +++ b/code/modules/mob/living/silicon/pai/pai.dm @@ -154,6 +154,8 @@ START_PROCESSING(SSfastprocess, src) GLOB.pai_list += src make_laws() + for (var/law in laws.inherent) + lawcheck += law if(!istype(pai_card)) //when manually spawning a pai, we create a card to put it into. var/newcardloc = pai_card pai_card = new /obj/item/paicard(newcardloc) diff --git a/code/modules/mob/living/silicon/robot/robot.dm b/code/modules/mob/living/silicon/robot/robot.dm index 75ce49fa112..b2df20eec1e 100644 --- a/code/modules/mob/living/silicon/robot/robot.dm +++ b/code/modules/mob/living/silicon/robot/robot.dm @@ -39,6 +39,8 @@ if(lawupdate) make_laws() + for (var/law in laws.inherent) + lawcheck += law if(!TryConnectToAI()) lawupdate = FALSE diff --git a/code/modules/mob/living/silicon/silicon.dm b/code/modules/mob/living/silicon/silicon.dm index f4a1930b2ff..69e28e45cab 100644 --- a/code/modules/mob/living/silicon/silicon.dm +++ b/code/modules/mob/living/silicon/silicon.dm @@ -27,9 +27,13 @@ var/list/alarm_types_show = list(ALARM_ATMOS = 0, ALARM_FIRE = 0, ALARM_POWER = 0, ALARM_CAMERA = 0, ALARM_MOTION = 0) var/list/alarm_types_clear = list(ALARM_ATMOS = 0, ALARM_FIRE = 0, ALARM_POWER = 0, ALARM_CAMERA = 0, ALARM_MOTION = 0) - var/lawcheck[1] - var/ioncheck[1] - var/hackedcheck[1] + //These lists will contain each law that should be announced / set to yes in the state laws menu. + ///List keeping track of which laws to announce + var/list/lawcheck = list() + ///List keeping track of hacked laws to announce + var/list/hackedcheck = list() + ///List keeping track of ion laws to announce + var/list/ioncheck = list() ///Are our siliconHUDs on? TRUE for yes, FALSE for no. var/sensors_on = TRUE @@ -61,6 +65,8 @@ ADD_TRAIT(src, TRAIT_NOFIRE_SPREAD, ROUNDSTART_TRAIT) ADD_TRAIT(src, TRAIT_ASHSTORM_IMMUNE, ROUNDSTART_TRAIT) + + /mob/living/silicon/Destroy() QDEL_NULL(radio) QDEL_NULL(aicamera) @@ -143,36 +149,48 @@ return TRUE return FALSE +/** + * Assembles all the zeroth, inherent and supplied laws into a single list. + */ +/mob/living/silicon/proc/assemble_laws() + var/list/laws_to_return = list() + laws_to_return += laws.zeroth + for (var/law in laws.inherent) + laws_to_return += law + for (var/law in laws.supplied) + if (law != "") // supplied laws start off with 15 blank strings, so don't add any of those + laws_to_return += law + return laws_to_return + /mob/living/silicon/Topic(href, href_list) - if (href_list["lawc"]) // Toggling whether or not a law gets stated by the State Laws verb --NeoFite - var/L = text2num(href_list["lawc"]) - switch(lawcheck[L+1]) - if ("Yes") - lawcheck[L+1] = "No" - if ("No") - lawcheck[L+1] = "Yes" + if (href_list["lawc"]) // Toggling whether or not a law gets stated by the State Laws verb + var/law_index = text2num(href_list["lawc"]) + var/law = assemble_laws()[law_index + 1] + if (law in lawcheck) + lawcheck -= law + else + lawcheck += law checklaws() - if (href_list["lawi"]) // Toggling whether or not a law gets stated by the State Laws verb --NeoFite - var/L = text2num(href_list["lawi"]) - switch(ioncheck[L]) - if ("Yes") - ioncheck[L] = "No" - if ("No") - ioncheck[L] = "Yes" + if (href_list["lawi"]) + var/law_index = text2num(href_list["lawi"]) + var/law = laws.ion[law_index] + if (law in ioncheck) + ioncheck -= law + else + ioncheck += law checklaws() if (href_list["lawh"]) - var/L = text2num(href_list["lawh"]) - switch(hackedcheck[L]) - if ("Yes") - hackedcheck[L] = "No" - if ("No") - hackedcheck[L] = "Yes" + var/law_index = text2num(href_list["lawh"]) + var/law = laws.hacked[law_index] + if (law in hackedcheck) + hackedcheck -= law + else + hackedcheck += law checklaws() - - if (href_list["laws"]) // With how my law selection code works, I changed statelaws from a verb to a proc, and call it through my law selection panel. --NeoFite + if (href_list["laws"]) statelaws() if (href_list["printlawtext"]) // this is kinda backwards @@ -198,97 +216,94 @@ var/forced_log_message = "stating laws[force ? ", forced" : ""]" //"radiomod" is inserted before a hardcoded message to change if and how it is handled by an internal radio. say("[radiomod] Current Active Laws:", forced = forced_log_message) - //laws_sanity_check() - //laws.show_laws(world) - var/number = 1 sleep(10) if (lawcache_zeroth) - if (force || lawcache_lawcheck[1] == "Yes") + if (force || (lawcache_zeroth in lawcache_lawcheck)) say("[radiomod] 0. [lawcache_zeroth]", forced = forced_log_message) sleep(10) for (var/index in 1 to length(lawcache_hacked)) var/law = lawcache_hacked[index] var/num = ion_num() - if (length(law) > 0) - if (force || lawcache_hackedcheck[index] == "Yes") - say("[radiomod] [num]. [law]", forced = forced_log_message) - sleep(10) + if (length(law) <= 0) + continue + if (force || (law in lawcache_hackedcheck)) + say("[radiomod] [num]. [law]", forced = forced_log_message) + sleep(10) for (var/index in 1 to length(lawcache_ion)) var/law = lawcache_ion[index] var/num = ion_num() - if (length(law) > 0) - if (force || lawcache_ioncheck[index] == "Yes") - say("[radiomod] [num]. [law]", forced = forced_log_message) - sleep(10) + if (length(law) <= 0) + return + if (force || (law in lawcache_ioncheck)) + say("[radiomod] [num]. [law]", forced = forced_log_message) + sleep(10) + var/number = 1 for (var/index in 1 to length(lawcache_inherent)) var/law = lawcache_inherent[index] - - if (length(law) > 0) - if (force || lawcache_lawcheck[index+1] == "Yes") - say("[radiomod] [number]. [law]", forced = forced_log_message) - number++ - sleep(10) + if (length(law) <= 0) + continue + if (force || (law in lawcache_lawcheck)) + say("[radiomod] [number]. [law]", forced = forced_log_message) + number++ + sleep(10) for (var/index in 1 to length(lawcache_supplied)) var/law = lawcache_supplied[index] - if (length(law) > 0) - if(length(lawcache_lawcheck) >= number+1) - if (force || lawcache_lawcheck[number+1] == "Yes") - say("[radiomod] [number]. [law]", forced = forced_log_message) - number++ - sleep(10) - - -/mob/living/silicon/proc/checklaws() //Gives you a link-driven interface for deciding what laws the statelaws() proc will share with the crew. --NeoFite + if (length(law) <= 0) + continue + if (force || (law in lawcache_lawcheck)) + say("[radiomod] [number]. [law]", forced = forced_log_message) + number++ + sleep(10) +///Gives you a link-driven interface for deciding what laws the statelaws() proc will share with the crew. +/mob/living/silicon/proc/checklaws() var/list = "Which laws do you want to include when stating them for the crew?

" + var/law_display = "Yes" if (laws.zeroth) - if (!lawcheck[1]) - lawcheck[1] = "No" //Given Law 0's usual nature, it defaults to NOT getting reported. --NeoFite - list += {"[lawcheck[1]] 0: [laws.zeroth]
"} + if (!(laws.zeroth in lawcheck)) + law_display = "No" + list += {"[law_display] 0: [laws.zeroth]
"} for (var/index in 1 to length(laws.hacked)) + law_display = "Yes" var/law = laws.hacked[index] if (length(law) > 0) - if (!hackedcheck[index]) - hackedcheck[index] = "No" - list += {"[hackedcheck[index]] [ion_num()]: [law]
"} - hackedcheck.len += 1 + if (!(law in hackedcheck)) + law_display = "No" + list += {"[law_display] [ion_num()]: [law]
"} for (var/index in 1 to length(laws.ion)) + law_display = "Yes" var/law = laws.ion[index] - if (length(law) > 0) - if (!ioncheck[index]) - ioncheck[index] = "Yes" - list += {"[ioncheck[index]] [ion_num()]: [law]
"} - ioncheck.len += 1 + if(!(law in ioncheck)) + law_display = "No" + list += {"[law_display] [ion_num()]: [law]
"} var/number = 1 for (var/index in 1 to length(laws.inherent)) + law_display = "Yes" var/law = laws.inherent[index] - if (length(law) > 0) - lawcheck.len += 1 - - if (!lawcheck[number+1]) - lawcheck[number+1] = "Yes" - list += {"[lawcheck[number+1]] [number]: [law]
"} + if (!(law in lawcheck)) + law_display = "No" + list += {"[law_display] [number]: [law]
"} number++ for (var/index in 1 to length(laws.supplied)) + law_display = "Yes" var/law = laws.supplied[index] if (length(law) > 0) - lawcheck.len += 1 - if (!lawcheck[number+1]) - lawcheck[number+1] = "Yes" - list += {"[lawcheck[number+1]] [number]: [law]
"} + if (!(law in lawcheck)) + law_display = "No" + list += {"[law_display] [number]: [law]
"} number++ list += {"

State Laws"}