2022-02-12 20:38:07 +03:00
|
|
|
module sim
|
|
|
|
|
|
|
|
import benchmark
|
|
|
|
import term
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub type SimRequestHandler = fn (request &SimRequest) !
|
2022-02-12 20:38:07 +03:00
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub type SimStartHandler = fn () !
|
2022-02-12 20:38:07 +03:00
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub type SimFinishHandler = fn () !
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
pub const (
|
|
|
|
default_width = 600
|
|
|
|
default_height = 600
|
|
|
|
)
|
|
|
|
|
|
|
|
[params]
|
|
|
|
pub struct GridSettings {
|
|
|
|
pub:
|
|
|
|
width int = sim.default_width
|
|
|
|
height int = sim.default_height
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_grid_settings(settings GridSettings) GridSettings {
|
|
|
|
return GridSettings{
|
|
|
|
...settings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[params]
|
|
|
|
pub struct RunnerSettings {
|
|
|
|
pub:
|
|
|
|
grid GridSettings
|
|
|
|
on_request SimRequestHandler
|
|
|
|
on_start SimStartHandler
|
|
|
|
on_finish SimFinishHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(params SimParams, settings RunnerSettings) {
|
|
|
|
height := settings.grid.height
|
|
|
|
width := settings.grid.width
|
|
|
|
|
|
|
|
if !isnil(settings.on_start) {
|
|
|
|
settings.on_start() or {
|
2022-11-15 16:53:13 +03:00
|
|
|
log(@MOD + '.' + @FN + ': Simulation start handler failed. Error ${err}')
|
2022-02-12 20:38:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut index := 0
|
|
|
|
log('')
|
|
|
|
|
|
|
|
mut bmark := benchmark.new_benchmark()
|
|
|
|
for y in 0 .. height {
|
|
|
|
$if verbose ? {
|
|
|
|
term.clear_previous_line()
|
|
|
|
}
|
|
|
|
log(@MOD + '.' + @FN + ': y: ${y + 1}')
|
|
|
|
for x in 0 .. width {
|
|
|
|
bmark.step()
|
|
|
|
// setup state conditions
|
|
|
|
position := vector(
|
|
|
|
x: 0.1 * ((f64(x) - 0.5 * f64(width - 1)) / f64(width - 1))
|
|
|
|
y: 0.1 * ((f64(y) - 0.5 * f64(height - 1)) / f64(height - 1))
|
|
|
|
z: 0.0
|
|
|
|
)
|
|
|
|
velocity := vector(x: 0, y: 0, z: 0)
|
|
|
|
|
|
|
|
mut state := new_state(
|
|
|
|
position: position
|
|
|
|
velocity: velocity
|
|
|
|
)
|
|
|
|
|
|
|
|
state.satisfy_rope_constraint(params)
|
|
|
|
request := &SimRequest{
|
|
|
|
id: index
|
|
|
|
state: state
|
|
|
|
params: params
|
|
|
|
}
|
|
|
|
settings.on_request(request) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
log(@MOD + '.' + @FN + ': request handler failed. Error ${err}')
|
2022-02-12 20:38:07 +03:00
|
|
|
bmark.fail()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
bmark.ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bmark.stop()
|
|
|
|
println(bmark.total_message(@FN))
|
|
|
|
|
|
|
|
if !isnil(settings.on_finish) {
|
|
|
|
settings.on_finish() or {
|
2022-11-15 16:53:13 +03:00
|
|
|
log(@MOD + '.' + @FN + ': Simulation stop handler failed. Error ${err}')
|
2022-02-12 20:38:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|