-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.lua
210 lines (176 loc) · 5.08 KB
/
loader.lua
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
local M = {}
local vec3 = require 'fsim.vec3'
local inertia = require 'fsim.inertia'
local rbody = require 'fsim.rigid_body'
local world = require 'fsim.world'
local object_name_store
local function clone_table ( def, skipset )
local p = {}
for k, v in pairs(def) do
if not (skipset and skipset[k]) then
p[k] = v
end
end
return p
end
local function parse_path ( s )
local p = {}
for w in string.gmatch(s, "([^%.]+)") do
p[#p+1] = w
end
return p
end
M.parse_loadworld = function ( wrld )
--local forces = {}
--local medium = {}
if wrld.randomseed then
print ('randomseed:', wrld.randomseed)
math.randomseed(wrld.randomseed)
end
local skiplist = {'IMPORT_BODY', 'BODY_FROM_NAME', 'V3', 'L', 'print'}
for _, s in ipairs(skiplist) do wrld[s] = nil end
-- link names if available
for i=1, #wrld.bodies do
local name = wrld.bodies[i].name
if name then
assert(not wrld.bodies[name], 'duplicated name: '..tostring(name))
wrld.bodies[name] = wrld.bodies[i]
end
end
for fidx, fdef in ipairs( wrld.forces or {} ) do
local class = fdef.class
local params = fdef.params
local name = params.name
print ('world force class', class, 'under name', name)
local path = parse_path(fdef.class)
local current = assert( require('fsim.'..path[1]) )
for i=2, #path-1 do
current = current[path[i]]
end
local f = current[path[#path]](params)
wrld.forces[fidx] = f
if name then
assert(not wrld.forces[name], 'duplicated name: '..tostring(name))
wrld.forces[name] = f
end
end
do
local medium = require 'fsim.medium'
wrld.medium = wrld.medium or {}
--surface
local surface = wrld.medium.surface or {generator='flat', params={h0=0}}
--[[
local surface_generator = surface.generator
local surface_params = surface.params
medium.surface = require'fsim.surface'[surface_generator](surface_params)
--]]
medium.surface = require'fsim.surface'.wavetrain_generate(surface.generator,surface.params)
--atmosphere
for k, v in pairs(wrld.medium.atmosphere or {}) do
medium.atmosphere[k] = v
end
--ocean
for k, v in pairs(wrld.medium.ocean or {}) do
medium.ocean[k] = v
end
wrld.medium = medium
end
--wrld = world.new ( wrld )
for k, v in pairs(wrld) do
if world[k] then print ('Overwriting world.'..k) end
world[k] = v
end
--return wrld
return world
end
M.parse_loadbody = function ( bdy )
local inertia_element
local forces
do
local inertia_elements = {}
for iidx, idef in ipairs( bdy.inertia or {} ) do
local class = idef.class
local params = idef.params
local name = params.name
local e = inertia.element.from_shape(class, params)
inertia_elements[iidx] = e
if name then
assert(not inertia_elements[name], 'duplicated name: '..tostring(name))
inertia_elements[name] = e
end
end
if #inertia_elements==1 then
inertia_element = inertia_elements[1]
else
inertia_element = inertia.element.from_elements(inertia_elements)
end
end
do
bdy.inertia = assert(inertia_element)
forces = bdy.forces or {} --store forces away for setting them after body creadion
bdy.forces = nil
bdy = rbody.new(bdy)
end
do
for fidx, fdef in ipairs( forces ) do
local class = fdef.class
local params = fdef.params
local name = params.name
params.body = bdy --!!!!!!!! implies one&only one exists
local path = parse_path(fdef.class)
local current = assert( require('fsim.'..path[1]) )
for i=2, #path-1 do
current = current[path[i]]
end
local f = current[path[#path]](params)
forces[fidx] = f
if name then
assert(not forces[name], 'duplicated name: '..tostring(name))
forces[name] = f
end
end
bdy.forces = forces
end
return bdy
end
local create_vector = function (v)
return vec3.new(v)
end
local import_body = function (filename, params)
local loadobject = {V3=create_vector}
local name = params.name
print( "importing body from", filename, 'as', name)
local cmd = assert( loadfile(filename, 'bt', loadobject) )
cmd()
-- overwrite fields from file object
for k, v in pairs(params) do
loadobject[k] = v
end
local b = M.parse_loadbody(loadobject)
assert(not object_name_store[name], 'duplicated name: '..tostring(name))
object_name_store[name] = b
return b
end
local body_from_name = function ( name )
return assert(object_name_store[name], 'unknown name: '..tostring(name))
end
M.load_body = function ( filename )
return import_body{ filename }
end
M.load_world = function ( filename )
local loadobject = {
IMPORT_BODY=import_body,
BODY_FROM_NAME=body_from_name,
V3=create_vector,
L=assert(_G),
print=print,
} -- check skiplist above
print( "loading world from", filename )
local cmd = assert( loadfile(filename, 'bt', loadobject) )
object_name_store = {}
cmd()
object_name_store = {}
local w = M.parse_loadworld(loadobject)
return w
end
return M