blob: 0a456ececc1e9542a8d7ea50dc2f54f869844e4a [file] [log] [blame] [raw]
package li.cil.oc.server.component
import java.io._
import li.cil.oc.Settings
import li.cil.oc.api.Network
import li.cil.oc.api.network._
import li.cil.oc.util.WirelessNetwork
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.TileEntity
import scala.collection.convert.WrapAsScala._
import scala.language.implicitConversions
class WirelessNetworkCard(val owner: TileEntity) extends NetworkCard {
override val node = Network.newNode(this, Visibility.Network).
withComponent("modem", Visibility.Neighbors).
withConnector().
create()
var strength = Settings.get.maxWirelessRange
// ----------------------------------------------------------------------- //
@LuaCallback(value = "getStrength", direct = true)
def getStrength(context: Context, args: Arguments): Array[AnyRef] = result(strength)
@LuaCallback("setStrength")
def setStrength(context: Context, args: Arguments): Array[AnyRef] = {
strength = math.max(args.checkDouble(0), math.min(0, Settings.get.maxWirelessRange))
result(strength)
}
override def isWireless(context: Context, args: Arguments): Array[AnyRef] = result(true)
override def send(context: Context, args: Arguments) = {
val address = args.checkString(0)
val port = checkPort(args.checkInteger(1))
checkPacketSize(args.drop(2))
if (strength > 0) {
checkPower()
for ((card, distance) <- WirelessNetwork.computeReachableFrom(this)
if card.node.address == address && card.openPorts.contains(port)) {
card.node.sendToReachable("computer.signal",
Seq("modem_message", node.address, Int.box(port), Double.box(distance)) ++ args.drop(2): _*)
}
}
super.send(context, args)
}
override def broadcast(context: Context, args: Arguments) = {
val port = checkPort(args.checkInteger(0))
checkPacketSize(args.drop(1))
if (strength > 0) {
checkPower()
for ((card, distance) <- WirelessNetwork.computeReachableFrom(this) if card.openPorts.contains(port)) {
card.node.sendToReachable("computer.signal",
Seq("modem_message", node.address, Int.box(port), Double.box(distance)) ++ args.drop(1): _*)
}
}
super.broadcast(context, args)
}
private def checkPower() {
val cost = Settings.get.wirelessCostPerRange
if (cost > 0 && !Settings.get.ignorePower) {
if (!node.tryChangeBuffer(-strength * cost)) {
throw new IOException("not enough energy")
}
}
}
// ----------------------------------------------------------------------- //
override val canUpdate = true
override def update() {
super.update()
WirelessNetwork.update(this)
}
override def onConnect(node: Node) {
super.onConnect(node)
if (node == this.node) {
WirelessNetwork.add(this)
}
}
override def onDisconnect(node: Node) {
super.onDisconnect(node)
if (node == this.node) {
val removed = WirelessNetwork.remove(this)
assert(removed)
}
}
// ----------------------------------------------------------------------- //
override def load(nbt: NBTTagCompound) {
super.load(nbt)
if (nbt.hasKey("strength")) {
strength = nbt.getDouble("strength") max 0 min Settings.get.maxWirelessRange
}
}
override def save(nbt: NBTTagCompound) {
super.save(nbt)
nbt.setDouble("strength", strength)
}
}