From 533ccef0ab7ad324e81bc049999a4012c9bcd2da Mon Sep 17 00:00:00 2001 From: PsiOmega Date: Tue, 10 Mar 2015 13:37:20 +0100 Subject: [PATCH] Robot code clean up and fixes. Fixes runtime when a borg is reset. Fixes issue where /proc/select_active_ai_with_fewest_borgs() would not always return the intended AI. Fixes issues with new borgs spamming the master AI with creation messages under some circumstances. --- code/__HELPERS/unsorted.dm | 2 +- code/datums/wires/robot.dm | 11 ++--- code/game/machinery/transformer.dm | 1 - code/game/objects/items/robot/robot_parts.dm | 1 - .../objects/items/robot/robot_upgrades.dm | 6 +-- .../mob/living/silicon/robot/drone/drone.dm | 2 - code/modules/mob/living/silicon/robot/life.dm | 4 +- .../modules/mob/living/silicon/robot/robot.dm | 45 ++++++++++++------- code/modules/mob/transform_procs.dm | 1 - code/modules/projectiles/projectile/change.dm | 1 - code/setup.dm | 6 +++ 11 files changed, 43 insertions(+), 37 deletions(-) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index ad8c771366..89da585924 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -374,7 +374,7 @@ Turf and target are seperate in case you want to teleport some distance from a t var/mob/living/silicon/ai/selected var/list/active = active_ais() for(var/mob/living/silicon/ai/A in active) - if(!selected || (selected.connected_robots > A.connected_robots)) + if(!selected || (selected.connected_robots.len > A.connected_robots.len)) selected = A return selected diff --git a/code/datums/wires/robot.dm b/code/datums/wires/robot.dm index 73555f3889..da20f1bd05 100644 --- a/code/datums/wires/robot.dm +++ b/code/datums/wires/robot.dm @@ -34,8 +34,7 @@ var/const/BORG_WIRE_CAMERA = 16 if (BORG_WIRE_AI_CONTROL) //Cut the AI wire to reset AI control if(!mended) - if (R.connected_ai) - R.connected_ai = null + R.disconnect_from_ai() if (BORG_WIRE_CAMERA) if(!isnull(R.camera) && !R.scrambledcodes) @@ -56,12 +55,8 @@ var/const/BORG_WIRE_CAMERA = 16 if (BORG_WIRE_AI_CONTROL) //pulse the AI wire to make the borg reselect an AI if(!R.emagged) var/mob/living/silicon/ai/new_ai = select_active_ai(R) - if(new_ai && (new_ai != R.connected_ai)) - R.connected_ai.connected_robots -= src - R.connected_ai = new_ai - new_ai.connected_robots += src - R.notify_ai(1) - R.sync() + R.connect_to_ai(new_ai) + if (BORG_WIRE_CAMERA) if(!isnull(R.camera) && R.camera.can_use() && !R.scrambledcodes) R.camera.kick_viewers() // Kick anyone watching the Cyborg's camera diff --git a/code/game/machinery/transformer.dm b/code/game/machinery/transformer.dm index 7885a8b8e2..c9c8ba20ae 100644 --- a/code/game/machinery/transformer.dm +++ b/code/game/machinery/transformer.dm @@ -38,7 +38,6 @@ playsound(src.loc, 'sound/machines/ping.ogg', 50, 0) if(robot) robot.SetLockDown(0) - robot.notify_ai(1) /obj/machinery/transformer/conveyor/New() ..() diff --git a/code/game/objects/items/robot/robot_parts.dm b/code/game/objects/items/robot/robot_parts.dm index 25461eae37..a0138b4dc6 100644 --- a/code/game/objects/items/robot/robot_parts.dm +++ b/code/game/objects/items/robot/robot_parts.dm @@ -226,7 +226,6 @@ feedback_inc("cyborg_birth",1) callHook("borgify", list(O)) - O.notify_ai(1) O.Namepick() del(src) diff --git a/code/game/objects/items/robot/robot_upgrades.dm b/code/game/objects/items/robot/robot_upgrades.dm index a9b040f647..3f11eb4bbe 100644 --- a/code/game/objects/items/robot/robot_upgrades.dm +++ b/code/game/objects/items/robot/robot_upgrades.dm @@ -36,12 +36,12 @@ icon = 'icons/mob/custom-synthetic.dmi' R.icon_state = "[R.ckey]-Standard" del(R.module) + R.notify_ai(ROBOT_NOTIFICATION_MODULE_RESET, R.module.name) R.module = null R.camera.remove_networks(list("Engineering","Medical","MINE")) R.updatename("Default") R.status_flags |= CANPUSH R.updateicon() - R.notify_ai(2) return 1 @@ -57,7 +57,7 @@ /obj/item/borg/upgrade/rename/action(var/mob/living/silicon/robot/R) if(..()) return 0 - R.notify_ai(3, R.name, heldname) + R.notify_ai(ROBOT_NOTIFICATION_NEW_NAME, R.name, heldname) R.name = heldname R.custom_name = heldname R.real_name = heldname @@ -84,7 +84,7 @@ R.stat = CONSCIOUS dead_mob_list -= R living_mob_list |= R - R.notify_ai(1) + R.notify_ai(ROBOT_NOTIFICATION_NEW_UNIT) return 1 diff --git a/code/modules/mob/living/silicon/robot/drone/drone.dm b/code/modules/mob/living/silicon/robot/drone/drone.dm index c1bfd9e009..9d2af7af56 100644 --- a/code/modules/mob/living/silicon/robot/drone/drone.dm +++ b/code/modules/mob/living/silicon/robot/drone/drone.dm @@ -54,8 +54,6 @@ /mob/living/silicon/robot/drone/init() laws = new /datum/ai_laws/drone() - connected_ai = null - aiCamera = new/obj/item/device/camera/siliconcam/drone_camera(src) playsound(src.loc, 'sound/machines/twobeep.ogg', 50, 0) diff --git a/code/modules/mob/living/silicon/robot/life.dm b/code/modules/mob/living/silicon/robot/life.dm index c5d74c54dd..c1d624f5b0 100644 --- a/code/modules/mob/living/silicon/robot/life.dm +++ b/code/modules/mob/living/silicon/robot/life.dm @@ -226,9 +226,7 @@ if(tra.current) var/I = image('icons/mob/mob.dmi', loc = tra.current, icon_state = "traitor") src.client.images += I - if(src.connected_ai) - src.connected_ai.connected_robots -= src - src.connected_ai = null + src.disconnect_from_ai() if(src.mind) if(!src.mind.special_role) src.mind.special_role = "traitor" diff --git a/code/modules/mob/living/silicon/robot/robot.dm b/code/modules/mob/living/silicon/robot/robot.dm index 3539e683af..3647352aab 100644 --- a/code/modules/mob/living/silicon/robot/robot.dm +++ b/code/modules/mob/living/silicon/robot/robot.dm @@ -164,11 +164,10 @@ var/list/robot_verbs_default = list( aiCamera = new/obj/item/device/camera/siliconcam/robot_camera(src) laws = new /datum/ai_laws/nanotrasen() additional_law_channels += "Binary" - connected_ai = select_active_ai_with_fewest_borgs() - if(connected_ai) - connected_ai.connected_robots += src + var/new_ai = select_active_ai_with_fewest_borgs() + if(new_ai) lawupdate = 1 - sync() + connect_to_ai(new_ai) else lawupdate = 0 @@ -365,7 +364,7 @@ var/list/robot_verbs_default = list( choose_icon(6,module_sprites) radio.config(module.channels) - notify_ai(2) + notify_ai(ROBOT_NOTIFICATION_NEW_MODULE, module.name) /mob/living/silicon/robot/proc/updatename(var/prefix as text) if(prefix) @@ -385,7 +384,7 @@ var/list/robot_verbs_default = list( else changed_name = "[modtype] [braintype]-[num2text(ident)]" - notify_ai(3, real_name, changed_name) + notify_ai(ROBOT_NOTIFICATION_NEW_NAME, real_name, changed_name) real_name = changed_name name = real_name @@ -802,7 +801,7 @@ var/list/robot_verbs_default = list( if(prob(50)) emagged = 1 lawupdate = 0 - connected_ai = null + disconnect_from_ai() user << "You emag [src]'s interface." message_admins("[key_name_admin(user)] emagged cyborg [key_name_admin(src)]. Laws overridden.") log_game("[key_name(user)] emagged cyborg [key_name(src)]. Laws overridden.") @@ -1134,8 +1133,7 @@ var/list/robot_verbs_default = list( return /mob/living/silicon/robot/proc/UnlinkSelf() - if (src.connected_ai) - src.connected_ai = null + disconnect_from_ai() lawupdate = 0 lockcharge = 0 canmove = 1 @@ -1245,14 +1243,29 @@ var/list/robot_verbs_default = list( return 1 return 0 -/mob/living/silicon/robot/proc/notify_ai(var/notifytype, var/oldname, var/newname) +/mob/living/silicon/robot/proc/notify_ai(var/notifytype, var/first_arg, var/second_arg) if(!connected_ai) return switch(notifytype) - if(1) //New Robot + if(ROBOT_NOTIFICATION_NEW_UNIT) //New Robot connected_ai << "

NOTICE - New [lowertext(braintype)] connection detected: [name]
" - if(2) //New Module - connected_ai << "

NOTICE - [braintype] module change detected: [name] has loaded the [module.name].
" - if(3) //New Name - if(oldname != newname) - connected_ai << "

NOTICE - [braintype] reclassification detected: [oldname] is now designated as [newname].
" + if(ROBOT_NOTIFICATION_NEW_MODULE) //New Module + connected_ai << "

NOTICE - [braintype] module change detected: [name] has loaded the [first_arg].
" + if(ROBOT_NOTIFICATION_MODULE_RESET) + connected_ai << "

NOTICE - [braintype] module reset detected: [name] has unladed the [first_arg].
" + if(ROBOT_NOTIFICATION_NEW_NAME) //New Name + if(first_arg != second_arg) + connected_ai << "

NOTICE - [braintype] reclassification detected: [first_arg] is now designated as [second_arg].
" + +/mob/living/silicon/robot/proc/disconnect_from_ai() + if(connected_ai) + connected_ai.connected_robots -= src + connected_ai = null + +/mob/living/silicon/robot/proc/connect_to_ai(var/mob/living/silicon/ai/AI) + if(AI && AI != connected_ai) + disconnect_from_ai() + connected_ai = AI + connected_ai.connected_robots |= src + notify_ai(ROBOT_NOTIFICATION_NEW_UNIT) + sync() diff --git a/code/modules/mob/transform_procs.dm b/code/modules/mob/transform_procs.dm index d6181f5ec7..227329e915 100644 --- a/code/modules/mob/transform_procs.dm +++ b/code/modules/mob/transform_procs.dm @@ -168,7 +168,6 @@ O.mmi.transfer_identity(src) callHook("borgify", list(O)) - O.notify_ai(1) O.Namepick() spawn(0)//To prevent the proc from returning null. diff --git a/code/modules/projectiles/projectile/change.dm b/code/modules/projectiles/projectile/change.dm index 24e2d4b6ef..f48a2d4a61 100644 --- a/code/modules/projectiles/projectile/change.dm +++ b/code/modules/projectiles/projectile/change.dm @@ -24,7 +24,6 @@ if(istype(M, /mob/living/silicon/robot)) var/mob/living/silicon/robot/Robot = M if(Robot.mmi) del(Robot.mmi) - Robot.notify_ai(1) else for(var/obj/item/W in M) if(istype(W, /obj/item/weapon/implant)) //TODO: Carn. give implants a dropped() or something diff --git a/code/setup.dm b/code/setup.dm index f47566d1eb..0a09af0cd4 100644 --- a/code/setup.dm +++ b/code/setup.dm @@ -762,3 +762,9 @@ var/list/be_special_flags = list( //General-purpose life speed define for plants. #define HYDRO_SPEED_MULTIPLIER 1 #define NANO_IGNORE_DISTANCE 1 + +// Robot AI notifications +#define ROBOT_NOTIFICATION_NEW_UNIT 1 +#define ROBOT_NOTIFICATION_NEW_NAME 2 +#define ROBOT_NOTIFICATION_NEW_MODULE 3 +#define ROBOT_NOTIFICATION_MODULE_RESET 4