Skip to content

Commit

Permalink
simple networks
Browse files Browse the repository at this point in the history
  • Loading branch information
silicons committed Sep 22, 2024
1 parent 102cb6f commit 2e8165d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 88 deletions.
9 changes: 0 additions & 9 deletions code/controllers/subsystem/networks/_networks.dm

This file was deleted.

19 changes: 0 additions & 19 deletions code/controllers/subsystem/networks/simple.dm

This file was deleted.

100 changes: 100 additions & 0 deletions code/controllers/subsystem/simple_networks.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//* This file is explicitly licensed under the MIT license. *//
//* Copyright (c) 2024 Citadel Station Developers *//

/**
* Just a simple way for things to send / receive data
*/
SUBSYSTEM_DEF(simple_networks)
name = "Simple Networks"
subsystem_flags = SS_NO_FIRE
// no init order for now
// no fire priority for now

// Simple networks
/// id lookup to a list of devices
var/static/list/simple_network_lookup = list()

/**
* Returns list of devices on a simple network.
*
* **WARNING: LIST IS NOT A COPY FOR PERFORMANCE - DO NOT MODIFY WITHOUT COPYING.**
*/
/datum/controller/subsystem/simple_networks/proc/get_devices(id)
return simple_network_lookup[id] || list()

/**
* Returns list of devices on a simple network within distance of atom
*/
/datum/controller/subsystem/simple_networks/proc/get_devices_in_range(id, atom/center, range = 25)
center = get_turf(center)
if(!center)
return list()
. = list()
for(var/atom/A in get_devices(id))
if(get_dist(A, center) <= range)
. += A

//* /datum API *//

/datum/proc/simple_network_register(id)
AddElement(/datum/element/simple_network, id)

/**
* Sends a simple network message.
*
* @params
* * id - id of network to send to
* * message - message to send or null
* * data - arbitrary list or null
*/
/datum/proc/simple_network_send(id, message, list/data)
var/list/devices = SSnetworks.get_devices(id)
for(var/datum/D as anything in devices)
D.simple_network_receive(id, message, data, src)

/**
* Called on receiving a simple network message - register to these by adding the element.
*/
/datum/proc/simple_network_receive(id, message, list/data, datum/sender)
return

//* /atom API *//

/**
* Sends a simple network message.
*
* @params
* * id - id of network to send to
* * message - message to send or null
* * data - arbitrary list or null
* * range - range from the current atom, defaults to ignoring.
*/
/atom/simple_network_send(id, message, list/data, range = INFINITY)
if(range == INFINITY)
return ..()
var/list/devices = SSsimple_networks.get_devices_in_range(id, src, range)
for(var/datum/D as anything in devices)
D.simple_network_receive(id, message, data, src)

//* Internal Element *//

/datum/element/simple_network
element_flags = ELEMENT_DETACH | ELEMENT_BESPOKE
id_arg_index = 1
var/id

/datum/element/simple_network/Attach(datum/target, id)
. = ..()
if(. & ELEMENT_INCOMPATIBLE)
return
src.id = id
if(SSnetworks.simple_network_lookup[id])
SSnetworks.simple_network_lookup[id] |= target
else
SSnetworks.simple_network_lookup[id] = list(target)

/datum/element/simple_network/Detach(datum/source)
SSnetworks.simple_network_lookup[id] -= source
if(!length(SSnetworks.simple_network_lookup[id]))
SSnetworks.simple_network_lookup -= id
return ..()
6 changes: 3 additions & 3 deletions code/game/objects/effects/traps.dm
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ Add those other swinging traps you mentioned above!

/obj/effect/trap/Initialize(mapload)
. = ..()
RegisterSimpleNetwork(id)
simple_network_register(id)

/obj/effect/trap/SimpleNetworkReceive(id, message, list/data, datum/sender)
/obj/effect/trap/simple_network_receive(id, message, list/data, datum/sender)
. = ..()
trip()

Expand Down Expand Up @@ -335,7 +335,7 @@ Add those other swinging traps you mentioned above!

/obj/effect/trap/launcher/Initialize(mapload)
. = ..()
RegisterSimpleNetwork(id)
simple_network_register(id)
START_PROCESSING(SSobj, src)

/obj/effect/trap/launcher/fire()
Expand Down
4 changes: 2 additions & 2 deletions code/game/turfs/simulated/flooring/flooring_traps.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
visible_message("<span class='danger'>You hear a click nearby!</span>")
update_icon()
playsound(src, 'sound/machines/click.ogg', 50, 1)
SimpleNetworkSend(id, "trip")
simple_network_send(id, "trip")
return

/turf/simulated/floor/trap/update_icon()
Expand Down Expand Up @@ -50,7 +50,7 @@
visible_message("<span class='danger'>You hear a click nearby!</span>")
update_icon()
playsound(src, 'sound/machines/click.ogg', 50, 1)
SimpleNetworkSend(id, "trip")
simple_network_send(id, "trip")
return

//Types
Expand Down
15 changes: 0 additions & 15 deletions code/modules/networks/simple/atom.dm

This file was deleted.

18 changes: 0 additions & 18 deletions code/modules/networks/simple/datum.dm

This file was deleted.

20 changes: 0 additions & 20 deletions code/modules/networks/simple/element.dm

This file was deleted.

2 changes: 0 additions & 2 deletions code/modules/networks/simple/helpers.dm

This file was deleted.

0 comments on commit 2e8165d

Please sign in to comment.