changed timing code a bit

This commit is contained in:
Redo 2020-04-01 18:04:46 -05:00
parent 6fce5f4271
commit ce22e04ec7

View File

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