forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cable_logic.dm
292 lines (214 loc) · 7.72 KB
/
cable_logic.dm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#define LOGIC_HIGH 5
//Indicators only have one input and no outputs
/obj/machinery/logic/indicator
//Input is searched from the 'dir' direction
var/obj/structure/cable/input
/obj/machinery/logic/indicator/process()
if(input)
return 1
if(!input)
var/turf/T = get_step(src, dir)
if(T)
var/inv_dir = turn(dir, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
input = C
return 1
return 0 //If it gets to here, it means no suitable wire to link to was found.
/obj/machinery/logic/indicator/bulb
icon = 'icons/obj/lighting.dmi'
icon_state = "bulb0"
/obj/machinery/logic/indicator/bulb/process()
if(!..()) //Parent proc checks if input1 exists.
return
var/datum/powernet/pn_input = input.powernet
if(!pn_input)
return
if(pn_input.avail >= LOGIC_HIGH)
icon_state = "bulb1"
else
icon_state = "bulb0"
//Sensors only have one output and no inputs
/obj/machinery/logic/sensor
//Output is searched from the 'dir' direction
var/obj/structure/cable/output
/obj/machinery/logic/sensor/process()
if(output)
return 1
if(!output)
var/turf/T = get_step(src, dir)
if(T)
var/inv_dir = turn(dir, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
output = C
return 1
return 0 //If it gets to here, it means no suitable wire to link to was found.
//Constant high generator. This will continue to send a signal of LOGIC_HIGH as long as it exists.
/obj/machinery/logic/sensor/constant_high
icon = 'icons/obj/atmospherics/outlet_injector.dmi'
icon_state = "off"
/obj/machinery/logic/sensor/constant_high/process()
if(!..()) //Parent proc checks if input1 exists.
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH)
//ONE INPUT logic elements have one input and one output
/obj/machinery/logic/oneinput
var/dir_input = 2
var/dir_output = 1
var/obj/structure/cable/input
var/obj/structure/cable/output
icon = 'icons/obj/pipes/heat.dmi'
icon_state = "intact"
/obj/machinery/logic/oneinput/process()
if(input && output)
return 1
if(!dir_input || !dir_output)
return 0
if(!input)
var/turf/T = get_step(src, dir_input)
if(T)
var/inv_dir = turn(dir_input, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
input = C
if(!output)
var/turf/T = get_step(src, dir_output)
if(T)
var/inv_dir = turn(dir_output, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
output = C
return 0 //On the process() call, where everything is still being searched for, it returns 0. It will return 1 on the next process() call.
//NOT GATE
/obj/machinery/logic/oneinput/not/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input = input.powernet
if(!pn_input)
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if( !(pn_input.avail >= LOGIC_HIGH))
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH) //Set the output avilable power to 5 or whatever it was before.
else
pn_output.newload += LOGIC_HIGH //Otherwise increase the load to 5
//TWO INPUT logic elements have two inputs and one output
/obj/machinery/logic/twoinput
var/dir_input1 = 2
var/dir_input2 = 8
var/dir_output = 1
var/obj/structure/cable/input1
var/obj/structure/cable/input2
var/obj/structure/cable/output
icon = 'icons/obj/atmospherics/mixer.dmi'
icon_state = "intact_off"
/obj/machinery/logic/twoinput/process()
if(input1 && input2 && output)
return 1
if(!dir_input1 || !dir_input2 || !dir_output)
return 0
if(!input1)
var/turf/T = get_step(src, dir_input1)
if(T)
var/inv_dir = turn(dir_input1, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
input1 = C
if(!input2)
var/turf/T = get_step(src, dir_input2)
if(T)
var/inv_dir = turn(dir_input2, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
input2 = C
if(!output)
var/turf/T = get_step(src, dir_output)
if(T)
var/inv_dir = turn(dir_output, 180)
for(var/obj/structure/cable/C in T)
if(C.d1 == inv_dir || C.d2 == inv_dir)
output = C
return 0 //On the process() call, where everything is still being searched for, it returns 0. It will return 1 on the next process() call.
//AND GATE
/obj/machinery/logic/twoinput/and/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input1 = input1.powernet
var/datum/powernet/pn_input2 = input2.powernet
if(!pn_input1 || !pn_input2)
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if( (pn_input1.avail >= LOGIC_HIGH) && (pn_input2.avail >= LOGIC_HIGH) )
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH) //Set the output avilable power to 5 or whatever it was before.
else
pn_output.newload += LOGIC_HIGH //Otherwise increase the load to 5
//OR GATE
/obj/machinery/logic/twoinput/or/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input1 = input1.powernet
var/datum/powernet/pn_input2 = input2.powernet
if(!pn_input1 || !pn_input2)
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if( (pn_input1.avail >= LOGIC_HIGH) || (pn_input2.avail >= LOGIC_HIGH) )
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH) //Set the output avilable power to 5 or whatever it was before.
else
pn_output.newload += LOGIC_HIGH //Otherwise increase the load to 5
//XOR GATE
/obj/machinery/logic/twoinput/xor/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input1 = input1.powernet
var/datum/powernet/pn_input2 = input2.powernet
if(!pn_input1 || !pn_input2)
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if( (pn_input1.avail >= LOGIC_HIGH) != (pn_input2.avail >= LOGIC_HIGH) )
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH) //Set the output avilable power to 5 or whatever it was before.
else
pn_output.newload += LOGIC_HIGH //Otherwise increase the load to 5
//XNOR GATE (EQUIVALENCE)
/obj/machinery/logic/twoinput/xnor/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input1 = input1.powernet
var/datum/powernet/pn_input2 = input2.powernet
if(!pn_input1 || !pn_input2)
return
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if( (pn_input1.avail >= LOGIC_HIGH) == (pn_input2.avail >= LOGIC_HIGH) )
pn_output.newavail = max(pn_output.avail, LOGIC_HIGH) //Set the output avilable power to 5 or whatever it was before.
else
pn_output.newload += LOGIC_HIGH //Otherwise increase the load to 5
#define RELAY_POWER_TRANSFER 2000 //How much power a relay transfers through.
//RELAY - input1 governs the flow from input2 to output
/obj/machinery/logic/twoinput/relay/process()
if(!..()) //Parent proc checks if input1, input2 and output exist.
return
var/datum/powernet/pn_input1 = input1.powernet
if(!pn_input1)
return
if( pn_input1.avail >= LOGIC_HIGH )
var/datum/powernet/pn_input2 = input2.powernet
var/datum/powernet/pn_output = output.powernet
if(!pn_output)
return
if(pn_input2.avail >= RELAY_POWER_TRANSFER)
pn_input2.newload += RELAY_POWER_TRANSFER
pn_output.newavail += RELAY_POWER_TRANSFER
#undef RELAY_POWER_TRANSFER
#undef LOGIC_HIGH