-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_lumen.lua
More file actions
122 lines (104 loc) · 3.02 KB
/
Copy pathstart_lumen.lua
File metadata and controls
122 lines (104 loc) · 3.02 KB
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
--require('mobdebug').coro()
local sched = require 'lumen.sched'
local loader = require 'fsim.loader'
local view = require 'fvis-three.visualize'.init() --sets sched.get_time to socket.gettime
--local view = require 'fvis-moongl.visualize'.init() --sets sched.get_time to sglfw.get_time
local w = loader.load_world('world/drop.lua')
--local w = loader.load_world('world/balloon.lua')
--local w = loader.load_world('world/flight.lua')
local total_run_time = 5 --math.huge --600
local fsim_step, fsim_run_time = 0.001, 0
local fvis_step, fvis_run_time = 1/10, 0
local fsim_write_step = nil --total_run_time/100
local fsim_count, fsim_over_count = 0, 0
-- physics
local function fsim_func ()
w.now = sched.get_time()
local next_t = w.now
local fsim_to_now = 0
local to_sleep = 0
local now, now2
while true do
now = sched.get_time()
next_t = next_t + fsim_step
if next_t<now then
--print ('fsim forwarding', next_t, now)
next_t = now
end
fsim_to_now = next_t - w.now
w:step(fsim_to_now)
now2 = sched.get_time()
fsim_run_time = fsim_run_time + now2 - now
fsim_count = fsim_count+1
to_sleep = next_t - now2
if to_sleep<0 then
--print ('WARN: fsim overstep', to_sleep, fsim_to_now)
fsim_over_count = fsim_over_count+1
to_sleep=0
end
sched.sleep(to_sleep)
end
end
-- display
local function fvis_func ()
local next_t, elapsed, sleep = sched.get_time(), 0, 0
local now
while true do
local now = sched.get_time()
view.display( w )
fvis_run_time = fvis_run_time + sched.get_time() - now
sched.sleep(fvis_step)
end
end
-- world write
local function wwrt_func ()
local next_t = sched.get_time()
while true do
w.write()
next_t = next_t + fsim_write_step
sched.sleep(next_t-sched.get_time())
end
end
--clock
local function tick_func ()
local next_t = sched.get_time()
while true do
print(sched.get_time())
next_t = next_t + 1
sched.sleep(next_t-sched.get_time())
end
end
-- main task
sched.run( function()
-- start tasks
local fsim_task = sched.run( fsim_func ):set_as_attached()
local fvis_task = sched.run( fvis_func ):set_as_attached()
--local tick_task = sched.run( tick_func ):set_as_attached()
local wwrt_task
if fsim_write_step then
wwrt_task = sched.run( wwrt_func ):set_as_attached()
end
-- wait for program termination
sched.sleep(total_run_time)
if fsim_write_step then
wwrt_task:kill()
w:close()
end
view.close()
fsim_task:kill()
fvis_task:kill()
print('fsim run time',fsim_run_time, 'of', total_run_time)
print('fsim count', fsim_count, 'over step', fsim_over_count)
print('fview run time',fvis_run_time, 'of', total_run_time)
sched.wait()
for t in pairs(sched.tasks) do
if t ~= sched.running_task then
print ('remaining task', t, sched.running_task)
--for k, v in pairs(t) do print ('=', k, v) end
--t:kill()
end
end
--os.exit()
end
)
sched.loop()