/
AnalogChips.kt
658 lines (542 loc) · 23.4 KB
/
AnalogChips.kt
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package mods.eln.sixnode
import mods.eln.Eln
import mods.eln.cable.CableRenderDescriptor
import mods.eln.gui.*
import mods.eln.i18n.I18N
import mods.eln.misc.*
import mods.eln.node.Node
import mods.eln.node.six.*
import mods.eln.sim.ElectricalLoad
import mods.eln.sim.IProcess
import mods.eln.sim.ThermalLoad
import mods.eln.sim.nbt.NbtElectricalGateInput
import mods.eln.sim.nbt.NbtElectricalGateOutput
import mods.eln.sim.nbt.NbtElectricalGateOutputProcess
import mods.eln.sixnode.SummingUnitElement.Companion.GainChangedEvents
import mods.eln.wiki.Data
import net.minecraft.client.gui.GuiScreen
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.client.IItemRenderer
import org.lwjgl.opengl.GL11
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.IOException
open class AnalogChipDescriptor(name: String, obj: Obj3D?, functionName: String,
functionClass: Class<out AnalogFunction>,
elementClass: Class<out AnalogChipElement>, renderClass: Class<out AnalogChipRender>):
SixNodeDescriptor(name, elementClass, renderClass) {
private val case = obj?.getPart("Case")
private val top = obj?.getPart(functionName)
private val pins = arrayOfNulls<Obj3D.Obj3DPart>(4)
internal val function = functionClass.newInstance()
init {
pins[0] = obj?.getPart("Output")
for (i in 1..function.inputCount) pins[i] = obj?.getPart("Input$i")
voltageLevelColor = VoltageLevelColor.SignalVoltage
}
constructor(name: String, obj: Obj3D?, functionName: String, functionClass: Class<out AnalogFunction>):
this(name, obj, functionName, functionClass, AnalogChipElement::class.java, AnalogChipRender::class.java) {}
fun draw() {
pins.forEach { it?.draw() }
case?.draw()
top?.draw()
}
override fun handleRenderType(item: ItemStack?, type: IItemRenderer.ItemRenderType?): Boolean = true
override fun shouldUseRenderHelper(type: IItemRenderer.ItemRenderType?, item: ItemStack?,
helper: IItemRenderer.ItemRendererHelper?): Boolean =
type != IItemRenderer.ItemRenderType.INVENTORY
override fun shouldUseRenderHelperEln(type: IItemRenderer.ItemRenderType?, item: ItemStack?,
helper: IItemRenderer.ItemRendererHelper?): Boolean =
type != IItemRenderer.ItemRenderType.INVENTORY
override fun renderItem(type: IItemRenderer.ItemRenderType?, item: ItemStack?, vararg data: Any?) {
if (type == IItemRenderer.ItemRenderType.INVENTORY) {
super.renderItem(type, item, *data)
} else {
GL11.glTranslatef(0.0f, 0.0f, -0.2f)
GL11.glScalef(1.25f, 1.25f, 1.25f)
GL11.glRotatef(-90.0f, 0.0f, 1.0f, 0.0f)
draw()
}
}
override fun getFrontFromPlace(side: Direction?, player: EntityPlayer?): LRDU? =
super.getFrontFromPlace(side, player).left()
override fun setParent(item: Item?, damage: Int) {
super.setParent(item, damage)
Data.addSignal(newItemStack())
}
override fun addInformation(itemStack: ItemStack?, entityPlayer: EntityPlayer?, list: MutableList<String>?, par4: Boolean) {
super.addInformation(itemStack, entityPlayer, list, par4)
if (list != null) {
function.infos.split("\n").forEach { list.add(it) }
}
}
}
open class AnalogChipElement(node: SixNode, side: Direction, sixNodeDescriptor: SixNodeDescriptor):
SixNodeElement(node, side, sixNodeDescriptor) {
private val descriptor = sixNodeDescriptor as AnalogChipDescriptor
private val outputPin = NbtElectricalGateOutput("output")
private val outputProcess = NbtElectricalGateOutputProcess("outputProcess", outputPin)
private val inputPins = arrayOfNulls<NbtElectricalGateInput>(3)
protected val function: AnalogFunction =
if (descriptor.function.hasState) descriptor.function.javaClass.newInstance()
else descriptor.function
init {
electricalLoadList.add(outputPin)
for (i in 0..descriptor.function.inputCount - 1) {
inputPins[i] = NbtElectricalGateInput("input$i")
electricalLoadList.add(inputPins[i])
}
electricalComponentList.add(outputProcess)
electricalProcessList.add(IProcess { time: Double ->
val inputs = arrayOfNulls<Double?>(3)
for (i in 0..2) {
val inputPin = inputPins[i]
if (inputPin != null && inputPin.connectedComponents.count() > 0) {
inputs[i] = inputPin.u
}
}
outputProcess.setUSafe(function.process(inputs, time))
})
}
override fun getElectricalLoad(lrdu: LRDU?): ElectricalLoad? = when (lrdu) {
front -> outputPin
front.inverse() -> inputPins[0]
front.left() -> inputPins[1]
front.right() -> inputPins[2]
else -> null
}
override fun getConnectionMask(lrdu: LRDU?): Int = when (lrdu) {
front -> Node.maskElectricalOutputGate
front.inverse() -> if (inputPins[0] != null) Node.maskElectricalInputGate else 0
front.left() -> if (inputPins[1] != null) Node.maskElectricalInputGate else 0
front.right() -> if (inputPins[2] != null) Node.maskElectricalInputGate else 0
else -> 0
}
override fun multiMeterString(): String? {
val builder = StringBuilder()
for (i in 1..3) {
val pin = inputPins[i - 1]
if (pin != null && pin.connectedComponents.count() > 0) {
builder.append("I$i: ").append(if (pin.stateLow()) "0"
else if (pin.stateHigh()) "1" else "?").append(", ")
}
}
builder.append(I18N.tr(" O: ")).append(if (outputProcess.u == 50.0) "1" else "0")
return builder.toString()
}
override fun getWaila(): MutableMap<String,String> = function.getWaila(
inputPins.map { if (it != null && it.connectedComponents.count() > 0) it.u else null }.toTypedArray(),
outputPin.u)
override fun onBlockActivated(entityPlayer: EntityPlayer?, side: Direction?, vx: Float, vy: Float, vz: Float): Boolean {
if (Utils.isPlayerUsingWrench(entityPlayer)) {
front = front.nextClockwise
sixNode.reconnect()
needPublish()
return true
} else {
return false
}
}
override fun readFromNBT(nbt: NBTTagCompound?) {
super.readFromNBT(nbt)
function.readFromNBT(nbt, "function")
}
override fun writeToNBT(nbt: NBTTagCompound?) {
super.writeToNBT(nbt)
function.writeToNBT(nbt, "function")
}
override fun getThermalLoad(lrdu: LRDU?): ThermalLoad? = null
override fun thermoMeterString(): String? = null
override fun initialize() {}
}
open class AnalogChipRender(entity: SixNodeEntity, side: Direction, descriptor: SixNodeDescriptor):
SixNodeElementRender(entity, side, descriptor) {
private val descriptor = descriptor as AnalogChipDescriptor
override fun draw() {
super.draw()
front.glRotateOnX()
descriptor.draw()
}
override fun getCableRender(lrdu: LRDU?): CableRenderDescriptor? = when(lrdu) {
front -> Eln.instance.signalCableDescriptor.render
front.inverse() -> if (descriptor.function.inputCount >= 1) Eln.instance.signalCableDescriptor.render else null
front.left() -> if (descriptor.function.inputCount >= 2) Eln.instance.signalCableDescriptor.render else null
front.right() -> if (descriptor.function.inputCount >= 3) Eln.instance.signalCableDescriptor.render else null
else -> null
}
}
abstract class AnalogFunction: INBTTReady {
companion object {
val inputColors = arrayOf("§c", "§a", "§9")
}
open val hasState = false
abstract val inputCount: Int
abstract val infos: String
internal fun Double.toDigital() = if (this <= 0.2) false
else if (this >= 0.6) true
else Math.random() > 0.5
internal fun Array<Double?>.toDigital(): List<Boolean?> = this.map { it?.toDigital() }
abstract fun process(inputs: Array<Double?>, deltaTime: Double): Double
open fun getWaila(inputs: Array<Double?>, output: Double) = mutableMapOf(
Pair("Inputs", (1..inputCount).map {"${inputColors[it - 1]}${Utils.plotVolt("", inputs[it -1] ?: 0.0)}"}.joinToString(" ")),
Pair("Output", Utils.plotVolt("", output))
)
override fun readFromNBT(nbt: NBTTagCompound?, str: String?) {}
override fun writeToNBT(nbt: NBTTagCompound?, str: String?) {}
}
class OpAmp: AnalogFunction() {
override val inputCount = 2
override val infos = I18N.tr("Operational Amplifier - DC coupled\nhigh-gain voltage amplifier with\ndifferential input. Can be used to\ncompare voltages or a configurable amplifier.")
override fun process(inputs: Array<Double?>, deltaTime: Double): Double =
10000 * ((inputs[0] ?: 0.0) - (inputs[1] ?: 0.0))
}
class PIDRegulator: AnalogFunction() {
override val hasState = true
override val inputCount = 2
override val infos = I18N.tr("Proportional–integral–derivative controller. A PID\ncontroller continuously calculates an error value as\nthe difference between a desired setpoint and a measured\nprocess variable and applies a correction based on\nproportional, integral, and derivative terms.")
internal var Kp = 1.0
internal var Ki = 0.0
set(value) {
field = value
errorIntegral = 0.0
}
internal var Kd = 0.0
private var lastError = 0.0
private var errorIntegral = 0.0
override fun process(inputs: Array<Double?>, deltaTime: Double): Double {
val error = (inputs[0] ?: 0.0) - (inputs[1] ?: 0.0)
errorIntegral += error * deltaTime
val result = Kp * error + Ki * errorIntegral + Kd * (error - lastError) / deltaTime
lastError = error
return result
}
override fun readFromNBT(nbt: NBTTagCompound?, str: String?) {
Kp = nbt?.getDouble("Kp") ?: 1.0
Ki = nbt?.getDouble("Ki") ?: 0.0
Kd = nbt?.getDouble("Kd") ?: 0.0
errorIntegral = nbt?.getDouble("errorIntegral") ?: 0.0
}
override fun writeToNBT(nbt: NBTTagCompound?, str: String?) {
nbt?.setDouble("Kp", Kp)
nbt?.setDouble("Ki", Ki)
nbt?.setDouble("Kd", Kd)
nbt?.setDouble("errorIntegral", errorIntegral)
}
override fun getWaila(inputs: Array<Double?>, output: Double): MutableMap<String, String> {
val info = super.getWaila(inputs, output)
info[I18N.tr("Params")] = "Kp = $Kp, Ki = $Ki, Kd = $Kd"
return info
}
}
class PIDRegulatorElement(node: SixNode, side: Direction, sixNodeDescriptor: SixNodeDescriptor):
AnalogChipElement(node, side, sixNodeDescriptor) {
companion object {
val KpParameterChangedEvent = 1
val KiParameterChangedEvent = 2
val KdParameterChangerEvent = 3
}
override fun hasGui() = true
override fun networkSerialize(stream: DataOutputStream?) {
super.networkSerialize(stream)
try {
with(function as PIDRegulator) {
stream?.writeFloat(Kp.toFloat())
stream?.writeFloat(Ki.toFloat())
stream?.writeFloat(Kd.toFloat())
}
} catch(e: IOException) {
e.printStackTrace()
}
}
override fun networkUnserialize(stream: DataInputStream?) {
super.networkUnserialize(stream)
try {
when (stream?.readByte()?.toInt()) {
KpParameterChangedEvent -> (function as PIDRegulator).Kp = stream?.readFloat()?.toDouble() ?: 0.0
KiParameterChangedEvent -> (function as PIDRegulator).Ki = stream?.readFloat()?.toDouble() ?: 0.0
KdParameterChangerEvent -> (function as PIDRegulator).Kd = stream?.readFloat()?.toDouble() ?: 0.0
}
needPublish()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class PIDRegulatorRender(entity: SixNodeEntity, side: Direction, descriptor: SixNodeDescriptor):
AnalogChipRender(entity, side, descriptor) {
internal var Kp = 1f
internal var Ki = 0f
internal var Kd = 0f
override fun newGuiDraw(side: Direction?, player: EntityPlayer?): GuiScreen? = PIDRegulatorGui(this)
override fun publishUnserialize(stream: DataInputStream?) {
super.publishUnserialize(stream)
try {
Kp = stream?.readFloat() ?: 1f
Ki = stream?.readFloat() ?: 0f
Kd = stream?.readFloat() ?: 0f
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class PIDRegulatorGui(val render: PIDRegulatorRender): GuiScreenEln() {
var KpBar: GuiVerticalTrackBar? = null
var KiBar: GuiVerticalTrackBar? = null
var KdBar: GuiVerticalTrackBar? = null
override fun initGui() {
super.initGui()
KpBar = newGuiVerticalTrackBar(10, 20, 20, 80)
KpBar?.setRange(0f, 20f)
KpBar?.setStepIdMax(20)
KpBar?.value = render.Kp
KiBar = newGuiVerticalTrackBar(40, 20, 20, 80)
KiBar?.value = render.Ki
KdBar = newGuiVerticalTrackBar(70, 20, 20, 80)
KdBar?.value = render.Kd
}
override fun preDraw(f: Float, x: Int, y: Int) {
super.preDraw(f, x, y)
KpBar?.setComment(0, KpBar?.value.toString())
KiBar?.setComment(0, KiBar?.value.toString())
KdBar?.setComment(0, KdBar?.value.toString())
}
override fun guiObjectEvent(`object`: IGuiObject?) {
try {
val bos = ByteArrayOutputStream()
val stream = DataOutputStream(bos)
render.preparePacketForServer(stream)
when (`object`) {
KpBar -> {
stream.writeByte(PIDRegulatorElement.KpParameterChangedEvent)
stream.writeFloat(KpBar?.value ?: 0f)
}
KiBar -> {
stream.writeByte(PIDRegulatorElement.KiParameterChangedEvent)
stream.writeFloat(KiBar?.value ?: 0f)
}
KdBar -> {
stream.writeByte(PIDRegulatorElement.KdParameterChangerEvent)
stream.writeFloat(KdBar?.value ?: 0f)
} else -> return
}
render.sendPacketToServer(bos)
} catch (e: IOException) {
e.printStackTrace()
}
}
override fun newHelper() = GuiHelper(this, 214, 118, "pid.png")
}
open class VoltageControlledSawtoothOscillator: AnalogFunction() {
override val hasState = true
override val inputCount = 1
override val infos = I18N.tr("A voltage-controlled oscillator or VCO is\nan electronic oscillator whose oscillation\nfrequency is controlled by a voltage input.")
private var out = 0.0
override fun process(inputs: Array<Double?>, deltaTime: Double): Double {
out += Math.pow(50.0, (inputs[0] ?: 0.0) / 50) * 2 * deltaTime
if (out > Eln.SVU) {
out = 0.0
}
return out
}
override fun readFromNBT(nbt: NBTTagCompound?, str: String?) {
out = nbt?.getDouble("out") ?: 0.0
}
override fun writeToNBT(nbt: NBTTagCompound?, str: String?) {
nbt?.setDouble("out", out)
}
}
class VoltageControlledSineOscillator: VoltageControlledSawtoothOscillator() {
override fun process(inputs: Array<Double?>, deltaTime: Double) =
25.0 + 25.0 * Math.sin(Math.PI * super.process(inputs, deltaTime) / 25.0)
}
class Amplifier: AnalogFunction() {
override val hasState = true
override val inputCount = 1
override val infos = I18N.tr("An amplifier increases the voltage\nof an input signal by a configurable\ngain and outputs that voltage.")
internal var gain = 1.0
override fun process(inputs: Array<Double?>, deltaTime: Double) = gain * (inputs[0] ?: 0.0)
override fun readFromNBT(nbt: NBTTagCompound?, str: String?) {
gain = nbt?.getDouble("gain") ?: 1.0
}
override fun writeToNBT(nbt: NBTTagCompound?, str: String?) {
nbt?.setDouble("gain", gain)
}
override fun getWaila(inputs: Array<Double?>, output: Double): MutableMap<String, String> {
val info = super.getWaila(inputs, output)
info["Gain"] = Utils.plotValue(gain)
return info
}
}
class AmplifierElement(node: SixNode, side: Direction, sixNodeDescriptor: SixNodeDescriptor):
AnalogChipElement(node, side, sixNodeDescriptor) {
companion object {
val GainChangedEvent = 1
}
override fun hasGui() = true
override fun networkSerialize(stream: DataOutputStream?) {
super.networkSerialize(stream)
try {
with(function as Amplifier) {
stream?.writeFloat(gain.toFloat())
}
} catch(e: IOException) {
e.printStackTrace()
}
}
override fun networkUnserialize(stream: DataInputStream?) {
super.networkUnserialize(stream)
try {
when (stream?.readByte()?.toInt()) {
GainChangedEvent -> (function as Amplifier).gain = stream?.readFloat()?.toDouble() ?: 0.0
}
needPublish()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class AmplifierRender(entity: SixNodeEntity, side: Direction, descriptor: SixNodeDescriptor):
AnalogChipRender(entity, side, descriptor) {
internal var gain = 1f
override fun newGuiDraw(side: Direction?, player: EntityPlayer?): GuiScreen? = AmplifierGui(this)
override fun publishUnserialize(stream: DataInputStream?) {
super.publishUnserialize(stream)
try {
gain = stream?.readFloat() ?: 1f
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class AmplifierGui(val render: AmplifierRender): GuiScreenEln() {
private var gainTF: GuiTextFieldEln? = null
override fun initGui() {
super.initGui()
gainTF = newGuiTextField(6, 6, 50)
gainTF?.setComment(0, I18N.tr("Gain"))
gainTF?.setText(render.gain)
gainTF?.setObserver { textField, text ->
try {
val bos = ByteArrayOutputStream()
val stream = DataOutputStream(bos)
render.preparePacketForServer(stream)
stream.writeByte(AmplifierElement.GainChangedEvent)
stream.writeFloat(text.toFloat())
render.sendPacketToServer(bos)
} catch (e: NumberFormatException) {
} catch (e: IOException) {
e.printStackTrace()
}
}
}
override fun newHelper() = GuiHelper(this, 62, 24)
}
class VoltageControlledAmplifier: AnalogFunction() {
override val inputCount = 2
override val infos = I18N.tr("A voltage-controlled amplifier (VCA)\nis an electronic amplifier that varies\nits gain depending on the control voltage.")
override fun process(inputs: Array<Double?>, deltaTime: Double) = (inputs[1] ?: 5.0) / 5.0 * (inputs[0] ?: 0.0)
override fun getWaila(inputs: Array<Double?>, output: Double): MutableMap<String, String> {
val info = super.getWaila(inputs, output)
info["Gain"] = Utils.plotValue((inputs[1] ?: 5.0) / 5.0)
return info
}
}
class SummingUnit() : AnalogFunction() {
override val hasState = true
override val inputCount = 3
override val infos = I18N.tr("The summing unit outputs the sum of\nthe tree wighted inputs at it's output.The\ngain for each input can be configured.")
internal val gains = arrayOf(1.0, 1.0, 1.0)
override fun process(inputs: Array<Double?>, deltaTime: Double) =
gains[0] * (inputs[0] ?: 0.0) + gains[1] * (inputs[1] ?: 0.0) + gains[2] * (inputs[2] ?: 0.0)
override fun readFromNBT(nbt: NBTTagCompound?, str: String?) {
for (i in gains.indices) {
gains[i] = nbt?.getDouble("gain$i") ?: 1.0
}
}
override fun writeToNBT(nbt: NBTTagCompound?, str: String?) {
for (i in gains.indices) {
nbt?.setDouble("gain$i", gains[i])
}
}
override fun getWaila(inputs: Array<Double?>, output: Double): MutableMap<String, String> {
val info = super.getWaila(inputs, output)
info["Gains"] = (0..2).map { "${inputColors[it]}${Utils.plotValue(gains[it])}" }.joinToString(" ")
return info
}
}
class SummingUnitElement(node: SixNode, side: Direction, sixNodeDescriptor: SixNodeDescriptor):
AnalogChipElement(node, side, sixNodeDescriptor) {
companion object {
val GainChangedEvents = arrayOf(1, 2, 3)
}
override fun hasGui() = true
override fun networkSerialize(stream: DataOutputStream?) {
super.networkSerialize(stream)
try {
with(function as SummingUnit) {
gains.forEach {
stream?.writeFloat(it.toFloat())
}
}
} catch(e: IOException) {
e.printStackTrace()
}
}
override fun networkUnserialize(stream: DataInputStream?) {
super.networkUnserialize(stream)
try {
when (stream?.readByte()?.toInt()) {
GainChangedEvents[0] -> (function as SummingUnit).gains[0] = stream?.readFloat()?.toDouble() ?: 1.0
GainChangedEvents[1] -> (function as SummingUnit).gains[1] = stream?.readFloat()?.toDouble() ?: 1.0
GainChangedEvents[2] -> (function as SummingUnit).gains[2] = stream?.readFloat()?.toDouble() ?: 1.0
}
needPublish()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class SummingUnitRender(entity: SixNodeEntity, side: Direction, descriptor: SixNodeDescriptor):
AnalogChipRender(entity, side, descriptor) {
internal var gains = floatArrayOf(1f, 1f, 1f)
override fun newGuiDraw(side: Direction?, player: EntityPlayer?): GuiScreen? = SummingUnitGui(this)
override fun publishUnserialize(stream: DataInputStream?) {
super.publishUnserialize(stream)
try {
for (i in gains.indices) { gains[i] = stream?.readFloat() ?: 1f }
} catch (e: IOException) {
e.printStackTrace()
}
}
}
class SummingUnitGui(val render: SummingUnitRender): GuiScreenEln() {
private var gainTFs = arrayOfNulls<GuiTextFieldEln>(3)
override fun initGui() {
super.initGui()
for (i in gainTFs.indices) {
gainTFs[i] = newGuiTextField(6, 6 + 20 * i, 50)
gainTFs[i]?.setText(render.gains[i])
gainTFs[i]?.setObserver { textField, text ->
try {
val bos = ByteArrayOutputStream()
val stream = DataOutputStream(bos)
render.preparePacketForServer(stream)
stream.writeByte(GainChangedEvents[i])
stream.writeFloat(text.toFloat())
render.sendPacketToServer(bos)
} catch (e: NumberFormatException) {
} catch (e: IOException) {
e.printStackTrace()
}
}
}
gainTFs[0]?.setComment(0, I18N.tr("Gain for input \u00a741"))
gainTFs[1]?.setComment(0, I18N.tr("Gain for input \u00a722"))
gainTFs[2]?.setComment(0, I18N.tr("Gain for input \u00a713"))
}
override fun newHelper() = GuiHelper(this, 62, 64)
}