mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-10 02:09:41 +00:00
Basic main structure for centralized alarm handling.
This commit is contained in:
78
code/modules/alarm/alarm.dm
Normal file
78
code/modules/alarm/alarm.dm
Normal file
@@ -0,0 +1,78 @@
|
||||
/datum/alarm_source
|
||||
var/source = null // The source trigger
|
||||
var/source_name = "" // The name of the source should it be lost (for example a destroyed camera)
|
||||
var/duration = 0 // How long this source will be alarming, 0 for indefinetely.
|
||||
var/start_time = 0 // When this source began alarming.
|
||||
var/end_time = 0 // Use to set when this trigger should clear, in case the source is lost.
|
||||
|
||||
/datum/alarm_source/New(var/atom/source)
|
||||
src.source = source
|
||||
source_name = source.name
|
||||
start_time = world.time
|
||||
|
||||
/datum/alarm
|
||||
var/atom/origin //Used to identify the alarm area.
|
||||
var/list/sources = new() //List of sources triggering the alarm. Used to determine when the alarm should be cleared.
|
||||
var/list/sources_assoc = new() //Associative list of source triggers. Used to efficiently acquire the alarm source.
|
||||
var/list/cameras //List of cameras that can be switched to, if the player has that capability.
|
||||
var/area/last_area //The last acquired area, used should origin be lost (for example a destroyed borg containing an alarming camera).
|
||||
|
||||
/datum/alarm/New(var/atom/origin, var/atom/source, var/duration)
|
||||
src.origin = origin
|
||||
last_area = alarm_area()
|
||||
set_duration(source, duration)
|
||||
|
||||
/datum/alarm/proc/set_duration(var/atom/source, var/duration)
|
||||
var/datum/alarm_source/AS = sources[source]
|
||||
if(!AS)
|
||||
AS = new/datum/alarm_source(source)
|
||||
sources += AS
|
||||
sources_assoc[source] = AS
|
||||
// Currently only non-0 durations can be altered (normal alarms VS EMP blasts)
|
||||
if(AS.duration)
|
||||
AS.duration = duration
|
||||
|
||||
/datum/alarm/proc/clear(var/source)
|
||||
var/datum/alarm_source/AS = sources[source]
|
||||
sources -= AS
|
||||
sources_assoc -= source
|
||||
|
||||
/datum/alarm/proc/alarm_area()
|
||||
if(!origin)
|
||||
return last_area
|
||||
|
||||
last_area = origin.get_alarm_area()
|
||||
return last_area
|
||||
|
||||
/datum/alarm/proc/cameras()
|
||||
if(!origin)
|
||||
return list()
|
||||
|
||||
if(!cameras)
|
||||
cameras = origin.get_alarm_cameras()
|
||||
|
||||
return cameras
|
||||
|
||||
|
||||
/atom/proc/get_alarm_area()
|
||||
return get_area(src)
|
||||
|
||||
/area/get_alarm_area()
|
||||
return src
|
||||
|
||||
/atom/proc/get_alarm_cameras()
|
||||
var/area/A = get_area(src)
|
||||
return A.get_cameras()
|
||||
|
||||
/area/get_alarm_cameras()
|
||||
return get_cameras()
|
||||
|
||||
/mob/living/silicon/robot/get_alarm_cameras()
|
||||
var/list/cameras = ..()
|
||||
if(camera)
|
||||
cameras += camera
|
||||
|
||||
return cameras
|
||||
|
||||
/mob/living/silicon/robot/syndicate/get_alarm_cameras()
|
||||
return list()
|
||||
112
code/modules/alarm/alarm_handler.dm
Normal file
112
code/modules/alarm/alarm_handler.dm
Normal file
@@ -0,0 +1,112 @@
|
||||
#define ALARM_ORIGIN_LOST "Origin Lost"
|
||||
|
||||
/datum/alarm_handler
|
||||
var/category = ""
|
||||
var/list/datum/alarm/alarms = new // All alarms, to handle cases when origin has been deleted with one or more active alarms
|
||||
var/list/datum/alarm/alarms_assoc = new // Associative list of alarms, to efficiently acquire them based on origin.
|
||||
|
||||
/datum/alarm_handler/proc/process()
|
||||
/*
|
||||
for(var/datum/alarm/A in alarms)
|
||||
var/datum/alarm_source/AS = A.source
|
||||
// Alarm owner has been deleted. Clean up in at most 15 seconds
|
||||
if(!AS.owner && !AS.end_time)
|
||||
AS.end_time = world.time + SecondsToTicks(15)
|
||||
if(AS.duration || AS.end_time)
|
||||
if(world.time > (AS.start_time + AS.duration) || world.time > AS.end_time)
|
||||
//Somethingthing..
|
||||
*/
|
||||
|
||||
/datum/alarm_handler/proc/triggerAlarm(var/atom/origin, var/atom/source, var/duration = 0)
|
||||
//Proper origin and source mandatory
|
||||
if(!origin || !source)
|
||||
return
|
||||
|
||||
//see if there is already an alarm of this origin
|
||||
var/alarm_key = origin.get_alarm_key()
|
||||
var/datum/alarm/existing = alarms_assoc[alarm_key]
|
||||
if(existing)
|
||||
existing.set_duration(source, duration)
|
||||
else
|
||||
existing = new/datum/alarm(origin, source, duration)
|
||||
|
||||
alarms |= existing
|
||||
alarms_assoc[alarm_key] = existing
|
||||
|
||||
/datum/alarm_handler/proc/cancelAlarm(var/atom/origin, var/source)
|
||||
//Proper origin and source mandatory
|
||||
if(!origin || !source)
|
||||
return
|
||||
|
||||
var/alarm_key = origin.get_alarm_key()
|
||||
|
||||
var/datum/alarm/existing = alarms_assoc[alarm_key]
|
||||
if(existing)
|
||||
existing.clear(source)
|
||||
if (!existing.sources.len)
|
||||
alarms -= existing
|
||||
alarms_assoc -= alarm_key
|
||||
|
||||
/atom/proc/get_alarm_key()
|
||||
return src
|
||||
|
||||
/turf/get_alarm_key()
|
||||
return get_area(src)
|
||||
|
||||
/obj/item/device/alarm_debug
|
||||
name = "An alarm debug tool - Self"
|
||||
desc = "Alarm Up. Alarm Reset."
|
||||
icon = 'icons/obj/radio.dmi'
|
||||
icon_state = "beacon"
|
||||
item_state = "signaler"
|
||||
|
||||
/obj/item/device/alarm_debug/loc
|
||||
name = "An alarm debug tool - Loc"
|
||||
|
||||
/obj/item/device/alarm_debug/verb/alarm()
|
||||
set name = "Alarm"
|
||||
set category = "Debug"
|
||||
usr << "Raising alarm"
|
||||
camera_alarm.triggerAlarm(src, src)
|
||||
|
||||
/obj/item/device/alarm_debug/verb/reset()
|
||||
set name = "Reset"
|
||||
set category = "Debug"
|
||||
usr << "Raising alarm"
|
||||
camera_alarm.triggerAlarm(src, src)
|
||||
|
||||
/obj/item/device/alarm_debug/verb/tell_me()
|
||||
set name = "Tell"
|
||||
set category = "Debug"
|
||||
usr << "Telling about alarms"
|
||||
|
||||
var/list/datum/alarm/alarms = camera_alarm.alarms
|
||||
var/list/datum/alarm/alarms_assoc = camera_alarm.alarms_assoc
|
||||
|
||||
world << "List"
|
||||
for(var/datum/alarm/A in alarms)
|
||||
world << "Origin: [A.origin ? A.origin : ALARM_ORIGIN_LOST]"
|
||||
world << "Alarm area: [A.alarm_area()]"
|
||||
for(var/source in A.sources)
|
||||
world << "Source: [source]"
|
||||
|
||||
world << "Assoc"
|
||||
|
||||
for(var/atom/origin in alarms_assoc)
|
||||
world << "Origin: [origin ? origin : ALARM_ORIGIN_LOST]"
|
||||
var/datum/alarm/A = alarms_assoc[origin]
|
||||
world << "Alarm area: [A.alarm_area()]"
|
||||
for(var/source in A.sources)
|
||||
world << "Source: [source]"
|
||||
|
||||
/obj/item/device/alarm_debug/loc/alarm()
|
||||
set name = "Alarm"
|
||||
set category = "Debug"
|
||||
usr << "Raising alarm"
|
||||
camera_alarm.triggerAlarm(src.loc, src)
|
||||
|
||||
/obj/item/device/alarm_debug/loc/reset()
|
||||
set name = "Reset"
|
||||
set category = "Debug"
|
||||
usr << "Clearing alarm"
|
||||
camera_alarm.cancelAlarm(src.loc, src)
|
||||
2
code/modules/alarm/atmosphere_alarm.dm
Normal file
2
code/modules/alarm/atmosphere_alarm.dm
Normal file
@@ -0,0 +1,2 @@
|
||||
/datum/alarm_handler/atmosphere
|
||||
category = "Atmosphere"
|
||||
2
code/modules/alarm/camera_alarm.dm
Normal file
2
code/modules/alarm/camera_alarm.dm
Normal file
@@ -0,0 +1,2 @@
|
||||
/datum/alarm_handler/camera
|
||||
category = "Camera"
|
||||
2
code/modules/alarm/fire_alarm.dm
Normal file
2
code/modules/alarm/fire_alarm.dm
Normal file
@@ -0,0 +1,2 @@
|
||||
/datum/alarm_handler/fire_alarm
|
||||
category = "Fire"
|
||||
2
code/modules/alarm/motion_alarm.dm
Normal file
2
code/modules/alarm/motion_alarm.dm
Normal file
@@ -0,0 +1,2 @@
|
||||
/datum/alarm_handler/motion
|
||||
category = "Motion"
|
||||
2
code/modules/alarm/power_alarm.dm
Normal file
2
code/modules/alarm/power_alarm.dm
Normal file
@@ -0,0 +1,2 @@
|
||||
/datum/alarm_handler/power
|
||||
category = "Power"
|
||||
Reference in New Issue
Block a user