77 lines
1.7 KiB
Lua
77 lines
1.7 KiB
Lua
|
|
local ffi = FFI
|
|
|
|
PortTypes = {
|
|
output = 0,
|
|
input = 1,
|
|
}
|
|
|
|
PortDirections = {
|
|
[0] = {-1, 0, 0},
|
|
[1] = {0, 1, 0},
|
|
[2] = {1, 0, 0},
|
|
[3] = {0, -1, 0},
|
|
[4] = {0, 0, 1},
|
|
[5] = {0, 0, -1}
|
|
}
|
|
|
|
Port = {}
|
|
|
|
function Port.new(type, direction, position, causeupdate, idx, gate)
|
|
local o = {
|
|
group = nil,
|
|
type = type,
|
|
direction = direction,
|
|
position = position,
|
|
causeupdate = causeupdate,
|
|
gate = gate,
|
|
idx = idx,
|
|
}
|
|
return o
|
|
end
|
|
|
|
function Port.getconnectionposition(port)
|
|
local offset = PortDirections[port.direction]
|
|
return {port.position[1]+offset[1], port.position[2]+offset[2], port.position[3]+offset[3]}
|
|
end
|
|
|
|
function Port.isrising(port)
|
|
return port.group.state[0]==1 and (port.group.update_tick[0] == GSim.current_tick)
|
|
end
|
|
|
|
function Port.isfalling(port)
|
|
return port.group.state[0]==0 and (port.group.update_tick[0] == GSim.current_tick)
|
|
end
|
|
|
|
function Port.getgate(port)
|
|
return port.gate
|
|
end
|
|
|
|
function Port.setgroup(port, group)
|
|
port.group = group
|
|
Port.getgate(port).port_nets[port.idx] = group
|
|
if group then
|
|
Port.getgate(port).port_net_state [port.idx] = group.state
|
|
Port.getgate(port).port_net_state_num[port.idx] = group.state_num
|
|
Port.getgate(port).port_net_in_queue [port.idx] = group.in_queue
|
|
Port.getgate(port).port_nets_c [port.idx] = group.c
|
|
else
|
|
Port.getgate(port).port_net_state [port.idx] = ffi.cast("int*", 0)
|
|
Port.getgate(port).port_net_state_num[port.idx] = ffi.cast("int*", 0)
|
|
Port.getgate(port).port_net_in_queue [port.idx] = ffi.cast("int*", 0)
|
|
Port.getgate(port).port_nets_c [port.idx] = ffi.cast("struct Net*", 0)
|
|
end
|
|
end
|
|
|
|
function Port.getgroup(port)
|
|
return port.group
|
|
end
|
|
|
|
function Port.gettype(port)
|
|
return port.type
|
|
end
|
|
|
|
function Port.getstate(port)
|
|
return Port.getgate(port).port_states[port.idx]
|
|
end
|