changed timing code a bit
This commit is contained in:
parent
6fce5f4271
commit
ce22e04ec7
90
sim/main.lua
90
sim/main.lua
@ -12,10 +12,13 @@ dofile("port.lua")
|
|||||||
dofile("save.lua")
|
dofile("save.lua")
|
||||||
|
|
||||||
OPT_TICK_ENABLED = true
|
OPT_TICK_ENABLED = true
|
||||||
OPT_TICK_TIME = 0
|
OPT_TICK_TIME = 0.001
|
||||||
OPT_FX_UPDATES = true
|
OPT_FX_UPDATES = true
|
||||||
OPT_FX_TIME = 0.03
|
OPT_FX_TIME = 0.03
|
||||||
|
|
||||||
|
local tickdelay = 0
|
||||||
|
local ticksperinterval = 0
|
||||||
|
|
||||||
bool_to_int = {[false] = 0, [true] = 1}
|
bool_to_int = {[false] = 0, [true] = 1}
|
||||||
|
|
||||||
local lastticktime = 0
|
local lastticktime = 0
|
||||||
@ -45,12 +48,12 @@ end
|
|||||||
local function unitize(v)
|
local function unitize(v)
|
||||||
local unit = 1
|
local unit = 1
|
||||||
v = v*1000000
|
v = v*1000000
|
||||||
|
|
||||||
while v >= 1000 do
|
while v >= 1000 do
|
||||||
v = v/1000
|
v = v/1000
|
||||||
unit = unit+1
|
unit = unit+1
|
||||||
end
|
end
|
||||||
|
|
||||||
local s
|
local s
|
||||||
if v >= 100 then
|
if v >= 100 then
|
||||||
s = "" .. round(v/10)*10
|
s = "" .. round(v/10)*10
|
||||||
@ -62,7 +65,7 @@ local function unitize(v)
|
|||||||
else
|
else
|
||||||
s = 0
|
s = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return s .. " " .. units[unit]
|
return s .. " " .. units[unit]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -109,7 +112,7 @@ acceptclient()
|
|||||||
|
|
||||||
while 1 do
|
while 1 do
|
||||||
local line, err = client:receive()
|
local line, err = client:receive()
|
||||||
|
|
||||||
if not err then
|
if not err then
|
||||||
local data = {}
|
local data = {}
|
||||||
local i = 1
|
local i = 1
|
||||||
@ -120,17 +123,17 @@ while 1 do
|
|||||||
data[i] = str or ""
|
data[i] = str or ""
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local i = 1
|
local i = 1
|
||||||
while i <= #data do
|
while i <= #data do
|
||||||
if data[i] == "W" then
|
if data[i] == "W" then
|
||||||
local min = vectotable(data[i+3])
|
local min = vectotable(data[i+3])
|
||||||
local max = vectotable(data[i+4])
|
local max = vectotable(data[i+4])
|
||||||
local bounds = {min[1], min[2], min[3], max[1], max[2], max[3]}
|
local bounds = {min[1], min[2], min[3], max[1], max[2], max[3]}
|
||||||
|
|
||||||
local wire = Wire:new(tonumber(data[i+1]), tonumber(data[i+2]), bounds)
|
local wire = Wire:new(tonumber(data[i+1]), tonumber(data[i+2]), bounds)
|
||||||
sim:addwire(wire)
|
sim:addwire(wire)
|
||||||
|
|
||||||
i = i + 4
|
i = i + 4
|
||||||
elseif data[i] == "G" then
|
elseif data[i] == "G" then
|
||||||
local objref = tonumber(data[i+1])
|
local objref = tonumber(data[i+1])
|
||||||
@ -138,12 +141,12 @@ while 1 do
|
|||||||
local position = vectotable(data[i+3])
|
local position = vectotable(data[i+3])
|
||||||
local rotation = tonumber(data[i+4])
|
local rotation = tonumber(data[i+4])
|
||||||
local gate = definition:constructgate(objref, position, rotation)
|
local gate = definition:constructgate(objref, position, rotation)
|
||||||
|
|
||||||
sim:addgate(gate)
|
sim:addgate(gate)
|
||||||
--print(gate.objref)
|
--print(gate.objref)
|
||||||
gate.definition.init(gate)
|
gate.definition.init(gate)
|
||||||
gate.definition.logic(gate)
|
gate.definition.logic(gate)
|
||||||
|
|
||||||
i = i + 4
|
i = i + 4
|
||||||
elseif data[i] == "RW" then
|
elseif data[i] == "RW" then
|
||||||
sim:removewire(tonumber(data[i+1]))
|
sim:removewire(tonumber(data[i+1]))
|
||||||
@ -162,7 +165,7 @@ while 1 do
|
|||||||
local global = data[i+7]
|
local global = data[i+7]
|
||||||
local numports = tonumber(data[i+8])
|
local numports = tonumber(data[i+8])
|
||||||
local ports = {}
|
local ports = {}
|
||||||
|
|
||||||
for a = i+9, numports*5+i+8, 5 do
|
for a = i+9, numports*5+i+8, 5 do
|
||||||
local port = {
|
local port = {
|
||||||
type = tonumber(data[a]),
|
type = tonumber(data[a]),
|
||||||
@ -172,39 +175,39 @@ while 1 do
|
|||||||
name = data[a+4],
|
name = data[a+4],
|
||||||
}
|
}
|
||||||
ports[#ports+1] = port
|
ports[#ports+1] = port
|
||||||
|
|
||||||
if not port.direction then print(line) end
|
if not port.direction then print(line) end
|
||||||
end
|
end
|
||||||
|
|
||||||
local definition = GateDefinition:new(objref, name, desc, init, logic, input, global, ports)
|
local definition = GateDefinition:new(objref, name, desc, init, logic, input, global, ports)
|
||||||
sim:addgatedefinition(definition)
|
sim:addgatedefinition(definition)
|
||||||
|
|
||||||
i = i + 8 + numports*5
|
i = i + 8 + numports*5
|
||||||
elseif data[i] == "SL" then
|
elseif data[i] == "SL" then
|
||||||
local wire = sim:getwirebyref(tonumber(data[i+1]))
|
local wire = sim:getwirebyref(tonumber(data[i+1]))
|
||||||
if wire ~= nil then
|
if wire ~= nil then
|
||||||
wire:setlayer(tonumber(data[i+2]))
|
wire:setlayer(tonumber(data[i+2]))
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 2
|
i = i + 2
|
||||||
elseif data[i] == "SP" then
|
elseif data[i] == "SP" then
|
||||||
local gate = sim:getgatebyref(tonumber(data[i+1]))
|
local gate = sim:getgatebyref(tonumber(data[i+1]))
|
||||||
if gate ~= nil then
|
if gate ~= nil then
|
||||||
gate.ports[tonumber(data[i+2])]:setstate(toboolean(data[i+3]))
|
gate.ports[tonumber(data[i+2])]:setstate(toboolean(data[i+3]))
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 3
|
i = i + 3
|
||||||
elseif data[i] == "SG" then
|
elseif data[i] == "SG" then
|
||||||
local wire = sim:getwirebyref(tonumber(data[i+1]))
|
local wire = sim:getwirebyref(tonumber(data[i+1]))
|
||||||
if wire ~= nil then
|
if wire ~= nil then
|
||||||
wire.group:setstate(toboolean(data[i+2]))
|
wire.group:setstate(toboolean(data[i+2]))
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 2
|
i = i + 2
|
||||||
elseif data[i] == "OPT" then
|
elseif data[i] == "OPT" then
|
||||||
local option = data[i+1]
|
local option = data[i+1]
|
||||||
local value = tonumber(data[i+2])
|
local value = tonumber(data[i+2])
|
||||||
|
|
||||||
if option == "TICK_ENABLED" then
|
if option == "TICK_ENABLED" then
|
||||||
OPT_TICK_ENABLED = toboolean(value)
|
OPT_TICK_ENABLED = toboolean(value)
|
||||||
elseif option == "TICK_TIME" then
|
elseif option == "TICK_TIME" then
|
||||||
@ -220,21 +223,21 @@ while 1 do
|
|||||||
end
|
end
|
||||||
OPT_FX_TIME = value
|
OPT_FX_TIME = value
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 2
|
i = i + 2
|
||||||
elseif data[i] == "GINFO" then
|
elseif data[i] == "GINFO" then
|
||||||
local userid = data[i+1]
|
local userid = data[i+1]
|
||||||
local objref = tonumber(data[i+2])
|
local objref = tonumber(data[i+2])
|
||||||
|
|
||||||
local obj = sim:getwirebyref(objref) or sim:getgatebyref(objref)
|
local obj = sim:getwirebyref(objref) or sim:getgatebyref(objref)
|
||||||
|
|
||||||
if obj ~= nil then
|
if obj ~= nil then
|
||||||
local info = ""
|
local info = ""
|
||||||
|
|
||||||
if obj.logictype == 0 then
|
if obj.logictype == 0 then
|
||||||
local numportsi = 0; for k, wire in pairs(obj.group.in_ports ) do numportsi = numportsi+1 end
|
local numportsi = 0; for k, wire in pairs(obj.group.in_ports ) do numportsi = numportsi+1 end
|
||||||
local numportso = 0; for k, wire in pairs(obj.group.out_ports) do numportso = numportso+1 end
|
local numportso = 0; for k, wire in pairs(obj.group.out_ports) do numportso = numportso+1 end
|
||||||
|
|
||||||
info = "\\c5WIRE<br>" .. (obj.group.state and "\\c2ON" or "\\c0OFF") .. "\n" ..
|
info = "\\c5WIRE<br>" .. (obj.group.state and "\\c2ON" or "\\c0OFF") .. "\n" ..
|
||||||
"IN PORTS: " ..numportsi.."\n"..
|
"IN PORTS: " ..numportsi.."\n"..
|
||||||
"OUT PORTS: "..numportso
|
"OUT PORTS: "..numportso
|
||||||
@ -245,12 +248,12 @@ while 1 do
|
|||||||
info = info .. (obj.ports[i].state and "\\c2" or "\\c0") .. obj.definition.ports[i].name .. (i ~= #obj.ports and " " or "")
|
info = info .. (obj.ports[i].state and "\\c2" or "\\c0") .. obj.definition.ports[i].name .. (i ~= #obj.ports and " " or "")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if info ~= "" then
|
if info ~= "" then
|
||||||
client:send("GINFO\t" .. userid .. "\t" .. expandescape(info) .. "\n")
|
client:send("GINFO\t" .. userid .. "\t" .. expandescape(info) .. "\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 2
|
i = i + 2
|
||||||
elseif data[i] == "SINFO" then
|
elseif data[i] == "SINFO" then
|
||||||
client:send("SINFO\t" .. data[i+1] .. "\t" .. sim.nwires .. "\t" .. sim.ngates .. "\t" .. sim.ninports .. "\t" .. sim.noutports .. "\n")
|
client:send("SINFO\t" .. data[i+1] .. "\t" .. sim.nwires .. "\t" .. sim.ngates .. "\t" .. sim.ninports .. "\t" .. sim.noutports .. "\n")
|
||||||
@ -270,7 +273,7 @@ while 1 do
|
|||||||
argv[#argv+1] = collapseescape(data[a])
|
argv[#argv+1] = collapseescape(data[a])
|
||||||
end
|
end
|
||||||
sim:queuegateinput(gate, argv)
|
sim:queuegateinput(gate, argv)
|
||||||
|
|
||||||
i = i+2+argc
|
i = i+2+argc
|
||||||
elseif data[i] == "SAVE" then
|
elseif data[i] == "SAVE" then
|
||||||
print("saving all data")
|
print("saving all data")
|
||||||
@ -278,47 +281,48 @@ while 1 do
|
|||||||
else
|
else
|
||||||
print("invalid data "..data[i])
|
print("invalid data "..data[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
elseif err == "closed" then
|
elseif err == "closed" then
|
||||||
sim = Simulation:new()
|
sim = Simulation:new()
|
||||||
acceptclient()
|
acceptclient()
|
||||||
end
|
end
|
||||||
|
|
||||||
local time = os.clock()
|
local time = os.clock()
|
||||||
|
|
||||||
if OPT_TICK_ENABLED then
|
if OPT_TICK_ENABLED then
|
||||||
if time - lastticktime >= OPT_TICK_TIME then
|
if time-lastticktime >= OPT_TICK_TIME then
|
||||||
sim:tick()
|
|
||||||
ticks = ticks + 1
|
|
||||||
lastticktime = time
|
lastticktime = time
|
||||||
|
|
||||||
local timetonext = time+OPT_TICK_TIME-os.clock()
|
sim:tick()
|
||||||
|
ticks = ticks+1
|
||||||
local sleeptime = timetonext*0.9
|
|
||||||
|
local sleeptime = time-os.clock()+OPT_TICK_TIME-0.03
|
||||||
if sleeptime>0 then socket.sleep(sleeptime) end
|
if sleeptime>0 then socket.sleep(sleeptime) end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
socket.sleep(0.1)
|
socket.sleep(0.05)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
lastticktime = time
|
||||||
|
|
||||||
if time-lastfxtime >= OPT_FX_TIME then
|
if time-lastfxtime >= OPT_FX_TIME then
|
||||||
sim:sendfxupdate()
|
sim:sendfxupdate()
|
||||||
sim:sendcallbacks()
|
sim:sendcallbacks()
|
||||||
lastfxtime = time
|
lastfxtime = time
|
||||||
end
|
end
|
||||||
|
|
||||||
if time-lastmeasuretime >= 0.1 then
|
if time-lastmeasuretime >= 0.1 then
|
||||||
if #avgticks >= 20 then
|
if #avgticks >= 10 then
|
||||||
totalticks = totalticks - table.remove(avgticks, 1)
|
totalticks = totalticks - table.remove(avgticks, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(avgticks, ticks)
|
table.insert(avgticks, ticks)
|
||||||
totalticks = totalticks + ticks
|
totalticks = totalticks + ticks
|
||||||
|
|
||||||
ticks = 0
|
ticks = 0
|
||||||
|
|
||||||
client:send("TPS\t" .. unitize((totalticks/#avgticks)/0.1) .. "\n")
|
client:send("TPS\t" .. unitize((totalticks/#avgticks)/0.1) .. "\n")
|
||||||
lastmeasuretime = os.clock()
|
lastmeasuretime = os.clock()
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user