[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 <supernovaa41@gmx.com>
This commit is contained in:
SkyratBot
2022-01-22 05:00:53 +00:00
committed by GitHub
co-authored by Seth Scherer
parent 21cb70365a
commit fad1bff5d6
6 changed files with 118 additions and 78 deletions
+4 -3
View File
@@ -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)
+4
View File
@@ -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)
+17 -1
View File
@@ -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)
@@ -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)
@@ -39,6 +39,8 @@
if(lawupdate)
make_laws()
for (var/law in laws.inherent)
lawcheck += law
if(!TryConnectToAI())
lawupdate = FALSE
+89 -74
View File
@@ -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 = "<b>Which laws do you want to include when stating them for the crew?</b><br><br>"
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 += {"<A href='byond://?src=[REF(src)];lawc=0'>[lawcheck[1]] 0:</A> <font color='#ff0000'><b>[laws.zeroth]</b></font><BR>"}
if (!(laws.zeroth in lawcheck))
law_display = "No"
list += {"<A href='byond://?src=[REF(src)];lawc=0'>[law_display] 0:</A> <font color='#ff0000'><b>[laws.zeroth]</b></font><BR>"}
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 += {"<A href='byond://?src=[REF(src)];lawh=[index]'>[hackedcheck[index]] [ion_num()]:</A> <font color='#660000'>[law]</font><BR>"}
hackedcheck.len += 1
if (!(law in hackedcheck))
law_display = "No"
list += {"<A href='byond://?src=[REF(src)];lawh=[index]'>[law_display] [ion_num()]:</A> <font color='#660000'>[law]</font><BR>"}
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 += {"<A href='byond://?src=[REF(src)];lawi=[index]'>[ioncheck[index]] [ion_num()]:</A> <font color='#547DFE'>[law]</font><BR>"}
ioncheck.len += 1
if(!(law in ioncheck))
law_display = "No"
list += {"<A href='byond://?src=[REF(src)];lawi=[index]'>[law_display] [ion_num()]:</A> <font color='#547DFE'>[law]</font><BR>"}
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 += {"<A href='byond://?src=[REF(src)];lawc=[number]'>[lawcheck[number+1]] [number]:</A> [law]<BR>"}
if (!(law in lawcheck))
law_display = "No"
list += {"<A href='byond://?src=[REF(src)];lawc=[index]'>[law_display] [number]:</A> [law]<BR>"}
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 += {"<A href='byond://?src=[REF(src)];lawc=[number]'>[lawcheck[number+1]] [number]:</A> <font color='#990099'>[law]</font><BR>"}
if (!(law in lawcheck))
law_display = "No"
list += {"<A href='byond://?src=[REF(src)];lawc=[number]'>[law_display] [number]:</A> <font color='#990099'>[law]</font><BR>"}
number++
list += {"<br><br><A href='byond://?src=[REF(src)];laws=1'>State Laws</A>"}