-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjbw_visualizer.cpp
634 lines (569 loc) · 22.4 KB
/
jbw_visualizer.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* Copyright 2019, The Jelly Bean World Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
#include "visualizer.h"
#include <thread>
#include <condition_variable>
#include <core/lex.h>
#if defined(WIN32)
#include <windows.h>
#endif
#include <signal.h>
using namespace jbw;
bool simulation_running = false;
std::atomic_bool* visualizer_running = nullptr;
#if defined(WIN32)
BOOL WINAPI signal_handler(DWORD sig_num)
{
if (sig_num == CTRL_C_EVENT || sig_num == CTRL_CLOSE_EVENT
|| sig_num == CTRL_LOGOFF_EVENT || sig_num == CTRL_SHUTDOWN_EVENT)
{
if (!simulation_running)
exit(EXIT_FAILURE);
if (visualizer_running != nullptr)
*visualizer_running = false;
simulation_running = false;
return TRUE;
}
return FALSE;
}
#else
void signal_handler(int sig_num) {
if (!simulation_running)
exit(EXIT_FAILURE);
if (visualizer_running != nullptr)
*visualizer_running = false;
simulation_running = false;
}
#endif
bool parse_address(
const char* arg, bool& fail,
char*& address, const char*& port)
{
if (address != nullptr || (arg[0] == '-' && arg[1] == '-'))
return false;
unsigned int colon_index = 0;
while (arg[colon_index] != ':' && arg[colon_index] != '\0')
colon_index++;
if (arg[colon_index] == '\0') {
fprintf(stderr, "ERROR: The server address must be of the form <address>:<port>.\n");
fail = true; return true;
}
address = (char*) malloc(sizeof(char) * (colon_index + 1));
if (address == nullptr) {
fprintf(stderr, "parse_address ERROR: Out of memory.\n");
fail = true; return true;
}
for (unsigned int i = 0; i < colon_index; i++)
address[i] = arg[i];
address[colon_index] = '\0';
port = arg + colon_index + 1;
return true;
}
inline bool parse_option(const char* arg,
bool& fail, const char* to_match)
{
return (strcmp(arg, to_match) == 0);
}
inline bool parse_option(
const char* arg, bool& fail,
const char* to_match, uint64_t& out)
{
size_t length = strlen(to_match);
if (strncmp(arg, to_match, length) != 0)
return false;
const char* option = arg + length;
unsigned long long value;
if (!parse_ulonglong(string(option), value)) {
fprintf(stderr, "ERROR: Unable to parse option '%s'.\n", arg);
fail = true; return true;
}
out = (uint64_t) value;
return true;
}
inline bool parse_option(
const char* arg, bool& fail,
const char* to_match, float& out)
{
size_t length = strlen(to_match);
if (strncmp(arg, to_match, length) != 0)
return false;
const char* option = arg + length;
double value;
if (!parse_float(string(option), value)) {
fprintf(stderr, "ERROR: Unable to parse option '%s'.\n", arg);
fail = true; return true;
}
out = (float) value;
return true;
}
template<typename Stream>
void print_usage(Stream&& out) {
fprintf(out, "Usage: jbw_visualizer <address>:<port> [options]\n"
"Connects to the JBW server at the given address visualizes the simulated environment.\n"
"\n"
"Available options:\n"
" --track=ID Starts tracking the agent with the given ID.\n"
" --pixels-per-cell=NUM Sets the initial number of pixels per cell.\n"
" --max-steps-per-sec=NUM Sets the maximum simulation steps per second.\n"
" --no-scent-map Disables drawing of the scent map.\n"
" --visual-field Draws the visual field around the tracked agent.\n"
" --agent-path Draws the movement path of the tracked agent. Note\n"
" that this will limit the simulation rate.\n"
" --local Starts a simulation locally, rather than connecting\n"
" to a server (any specified address is ignored).\n"
" --help Prints this usage text.\n");
}
template<typename Stream>
void print_controls(Stream&& out) {
fprintf(out, "\nControls:\n"
"Click and drag with left mouse button to move camera.\n"
" + key: Zoom in.\n"
" - key: Zoom out.\n"
" [ key: Decrease max simulation steps per second.\n"
" ] key: Increase max simulation steps per second.\n"
" b key: Toggle drawing of the scent map.\n"
" v key: Toggle drawing of the agent's visual field.\n"
" p key: Toggle drawing of the agent's path.\n"
" s key: Save screenshot to 'screenshotN.svg' where N is the smallest integer\n"
" such that the file does not already exist in the current directory.\n"
" 1 key: Track agent with ID 1.\n"
" 2 key: Track agent with ID 2.\n"
" 3 key: Track agent with ID 3.\n"
" 4 key: Track agent with ID 4.\n"
" 5 key: Track agent with ID 5.\n"
" 6 key: Track agent with ID 6.\n"
" 7 key: Track agent with ID 7.\n"
" 8 key: Track agent with ID 8.\n"
" 9 key: Track agent with ID 9.\n"
" 0 key: Disable agent tracking.\n\n");
}
inline void set_interaction_args(
item_properties* item_types, unsigned int first_item_type,
unsigned int second_item_type, interaction_function interaction,
std::initializer_list<float> args)
{
item_types[first_item_type].interaction_fns[second_item_type].fn = interaction;
item_types[first_item_type].interaction_fns[second_item_type].arg_count = (unsigned int) args.size();
item_types[first_item_type].interaction_fns[second_item_type].args = (float*) malloc(max((size_t) 1, sizeof(float) * args.size()));
unsigned int counter = 0;
for (auto i = args.begin(); i != args.end(); i++)
item_types[first_item_type].interaction_fns[second_item_type].args[counter++] = *i;
}
struct visualizer_data {
bool waiting_for_server;
std::condition_variable cv;
std::mutex lock;
visualizer_data() : waiting_for_server(false) { }
visualizer_data(const visualizer_data& src) : waiting_for_server(src.waiting_for_server) { }
};
inline void on_step(simulator<visualizer_data>* sim,
const hash_map<uint64_t, agent_state*>& agents, uint64_t time)
{
visualizer_data& data = sim->get_data();
std::unique_lock<std::mutex> lock(data.lock);
data.waiting_for_server = false;
data.cv.notify_one();
#if defined(RECORD)
record_step();
#endif
}
template<typename PerPatchData, typename ItemType>
void generate_map(
map<PerPatchData, ItemType>& world,
const jbw::position& bottom_left_corner,
const jbw::position& top_right_corner)
{
/* make sure enough of the world is generated */
patch<PerPatchData>* neighborhood[4]; jbw::position patch_positions[4];
for (int64_t x = bottom_left_corner.x; x <= top_right_corner.x; x += world.n) {
for (int64_t y = bottom_left_corner.y; y <= top_right_corner.y; y += world.n)
world.get_fixed_neighborhood(jbw::position(x, y), neighborhood, patch_positions);
world.get_fixed_neighborhood(jbw::position(x, top_right_corner.y), neighborhood, patch_positions);
}
for (int64_t y = bottom_left_corner.y; y <= top_right_corner.y; y += world.n)
world.get_fixed_neighborhood(jbw::position(top_right_corner.x, y), neighborhood, patch_positions);
world.get_fixed_neighborhood(top_right_corner, neighborhood, patch_positions);
}
uint64_t simulation_time;
#if defined(RECORD)
FILE* log_file = nullptr;
unsigned int frame_number = 0;
unsigned int total_frame_number = 0;
unsigned int* collected_items = nullptr;
unsigned int collected_item_count = 0;
uint64_t old_simulation_time = UINT64_MAX;
array<char> key_presses(8);
bool step;
namespace jbw {
inline bool record_key_press(char key) {
return key_presses.add(key);
}
inline bool record_step() {
step = true;
return false;
}
/* NOTE: this function assumes the number of item types in the environment does not change */
inline bool record_collected_items(const unsigned int* src, unsigned int count) {
if (collected_items == nullptr) {
collected_item_count = count;
collected_items = (unsigned int*) malloc(sizeof(unsigned int) * count);
if (collected_items == nullptr)
return false;
}
for (unsigned int i = 0; i < count; i++)
collected_items[i] = src[i];
return true;
}
const char empty_string[] = "";
const char comma_string[] = ",";
const char space_string[] = " ";
inline void increment_frame_number() {
if (step) {
simulation_time++;
step = false;
}
total_frame_number++;
}
inline void write_to_log() {
if (simulation_time != old_simulation_time) {
fprintf(log_file, "%u sim_time:%lu", frame_number, simulation_time);
core::print<unsigned int, space_string, empty_string, comma_string>(collected_items, collected_item_count, log_file);
if (key_presses.length > 0)
core::print<char, space_string, empty_string, comma_string>(key_presses, log_file);
core::print('\n', log_file); fflush(log_file);
key_presses.clear();
old_simulation_time = simulation_time;
} else if (key_presses.length > 0) {
fprintf(log_file, "%u", frame_number);
core::print<char, space_string, empty_string, comma_string>(key_presses, log_file);
core::print('\n', log_file); fflush(log_file);
key_presses.clear();
}
frame_number++;
}
} /* namespace jbw */
#endif
namespace jbw {
#if defined(RECORD)
inline unsigned long long milliseconds() {
return (1000 * total_frame_number) / 60;
}
#else
inline unsigned long long milliseconds() {
return core::milliseconds();
}
#endif
}
bool run_locally(
uint64_t track_agent_id,
float pixels_per_cell,
bool draw_scent_map,
bool draw_visual_field,
bool draw_agent_path,
float max_steps_per_second)
{
simulator_config config;
config.max_steps_per_movement = 1;
config.scent_dimension = 3;
config.color_dimension = 3;
config.vision_range = 5;
config.agent_field_of_view = 2 * M_PI;
config.allowed_movement_directions[0] = action_policy::ALLOWED;
config.allowed_movement_directions[1] = action_policy::DISALLOWED;
config.allowed_movement_directions[2] = action_policy::DISALLOWED;
config.allowed_movement_directions[3] = action_policy::DISALLOWED;
config.allowed_rotations[0] = action_policy::DISALLOWED;
config.allowed_rotations[1] = action_policy::DISALLOWED;
config.allowed_rotations[2] = action_policy::ALLOWED;
config.allowed_rotations[3] = action_policy::ALLOWED;
config.no_op_allowed = false;
config.patch_size = 32;
config.mcmc_iterations = 4000;
config.agent_color = (float*) calloc(config.color_dimension, sizeof(float));
config.agent_color[2] = 1.0f;
config.collision_policy = movement_conflict_policy::FIRST_COME_FIRST_SERVED;
config.decay_param = 0.4f;
config.diffusion_param = 0.14f;
config.deleted_item_lifetime = 2000;
/* configure item types */
unsigned int item_type_count = 4;
config.item_types.ensure_capacity(item_type_count);
config.item_types[0].name = "banana";
config.item_types[0].scent = (float*) calloc(config.scent_dimension, sizeof(float));
config.item_types[0].color = (float*) calloc(config.color_dimension, sizeof(float));
config.item_types[0].required_item_counts = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[0].required_item_costs = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[0].scent[1] = 1.0f;
config.item_types[0].color[1] = 1.0f;
config.item_types[0].required_item_counts[0] = 1;
config.item_types[0].blocks_movement = false;
config.item_types[0].visual_occlusion = 0.0;
config.item_types[1].name = "onion";
config.item_types[1].scent = (float*) calloc(config.scent_dimension, sizeof(float));
config.item_types[1].color = (float*) calloc(config.color_dimension, sizeof(float));
config.item_types[1].required_item_counts = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[1].required_item_costs = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[1].scent[0] = 1.0f;
config.item_types[1].color[0] = 1.0f;
config.item_types[1].required_item_counts[1] = 1;
config.item_types[1].blocks_movement = false;
config.item_types[1].visual_occlusion = 0.0;
config.item_types[2].name = "jellybean";
config.item_types[2].scent = (float*) calloc(config.scent_dimension, sizeof(float));
config.item_types[2].color = (float*) calloc(config.color_dimension, sizeof(float));
config.item_types[2].required_item_counts = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[2].required_item_costs = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[2].scent[2] = 1.0f;
config.item_types[2].color[2] = 1.0f;
config.item_types[2].blocks_movement = false;
config.item_types[2].visual_occlusion = 0.0;
config.item_types[3].name = "wall";
config.item_types[3].scent = (float*) calloc(config.scent_dimension, sizeof(float));
config.item_types[3].color = (float*) calloc(config.color_dimension, sizeof(float));
config.item_types[3].required_item_counts = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[3].required_item_costs = (unsigned int*) calloc(item_type_count, sizeof(unsigned int));
config.item_types[3].color[0] = 0.5f;
config.item_types[3].color[1] = 0.5f;
config.item_types[3].color[2] = 0.5f;
config.item_types[3].required_item_counts[3] = 1;
config.item_types[3].blocks_movement = true;
config.item_types[3].visual_occlusion = 0.0;
config.item_types.length = item_type_count;
config.item_types[0].intensity_fn.fn = constant_intensity_fn;
config.item_types[0].intensity_fn.arg_count = 1;
config.item_types[0].intensity_fn.args = (float*) malloc(sizeof(float) * 1);
config.item_types[0].intensity_fn.args[0] = -5.3f;
config.item_types[0].interaction_fns = (energy_function<interaction_function>*)
malloc(sizeof(energy_function<interaction_function>) * config.item_types.length);
config.item_types[1].intensity_fn.fn = constant_intensity_fn;
config.item_types[1].intensity_fn.arg_count = 1;
config.item_types[1].intensity_fn.args = (float*) malloc(sizeof(float) * 1);
config.item_types[1].intensity_fn.args[0] = -5.0f;
config.item_types[1].interaction_fns = (energy_function<interaction_function>*)
malloc(sizeof(energy_function<interaction_function>) * config.item_types.length);
config.item_types[2].intensity_fn.fn = constant_intensity_fn;
config.item_types[2].intensity_fn.arg_count = 1;
config.item_types[2].intensity_fn.args = (float*) malloc(sizeof(float) * 1);
config.item_types[2].intensity_fn.args[0] = -5.3f;
config.item_types[2].interaction_fns = (energy_function<interaction_function>*)
malloc(sizeof(energy_function<interaction_function>) * config.item_types.length);
config.item_types[3].intensity_fn.fn = constant_intensity_fn;
config.item_types[3].intensity_fn.arg_count = 1;
config.item_types[3].intensity_fn.args = (float*) malloc(sizeof(float) * 1);
config.item_types[3].intensity_fn.args[0] = 0.0f;
config.item_types[3].interaction_fns = (energy_function<interaction_function>*)
malloc(sizeof(energy_function<interaction_function>) * config.item_types.length);
set_interaction_args(config.item_types.data, 0, 0, piecewise_box_interaction_fn, {10.0f, 200.0f, 0.0f, -6.0f});
set_interaction_args(config.item_types.data, 0, 1, piecewise_box_interaction_fn, {200.0f, 0.0f, -6.0f, -6.0f});
set_interaction_args(config.item_types.data, 0, 2, piecewise_box_interaction_fn, {10.0f, 200.0f, 2.0f, -100.0f});
set_interaction_args(config.item_types.data, 0, 3, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 1, 0, piecewise_box_interaction_fn, {200.0f, 0.0f, -6.0f, -6.0f});
set_interaction_args(config.item_types.data, 1, 1, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 1, 2, piecewise_box_interaction_fn, {200.0f, 0.0f, -100.0f, -100.0f});
set_interaction_args(config.item_types.data, 1, 3, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 2, 0, piecewise_box_interaction_fn, {10.0f, 200.0f, 2.0f, -100.0f});
set_interaction_args(config.item_types.data, 2, 1, piecewise_box_interaction_fn, {200.0f, 0.0f, -100.0f, -100.0f});
set_interaction_args(config.item_types.data, 2, 2, piecewise_box_interaction_fn, {10.0f, 200.0f, 0.0f, -6.0f});
set_interaction_args(config.item_types.data, 2, 3, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 3, 0, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 3, 1, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 3, 2, zero_interaction_fn, {});
set_interaction_args(config.item_types.data, 3, 3, cross_interaction_fn, {10.0f, 15.0f, 20.0f, -200.0f, -20.0f, 1.0f});
simulator<visualizer_data> sim(config, visualizer_data());
uint64_t agent_id; agent_state* agent;
if (sim.add_agent(agent_id, agent) != status::OK) {
fprintf(stderr, "run_locally ERROR: Unable to add new agent.\n");
return false;
}
#if defined(RECORD)
log_file = (FILE*) fopen("recording.log", "wb");
#endif
print_controls(stdout); fflush(stdout);
simulation_running = true;
bool fullscreen = false;
#if defined(RECORD)
fullscreen = true;
#endif
visualizer<simulator<visualizer_data>> visualizer(sim, 2560, 1440, track_agent_id,
pixels_per_cell, draw_scent_map, draw_visual_field, draw_agent_path, max_steps_per_second, fullscreen);
unsigned int move_count = 0;
std::thread simulation_worker = std::thread([&]() {
while (simulation_running) {
auto action = rand() % 20;
status result = status::OK;
sim.get_data().waiting_for_server = true;
if (action % 20 == 0) {
result = sim.turn(agent_id, direction::RIGHT);
} else {
result = sim.move(agent_id, direction::UP, 1);
}
if (result != status::OK) {
fprintf(stderr, "run_locally ERROR: Unable to perform agent action.\n");
break;
}
move_count++;
std::unique_lock<std::mutex> lock(sim.get_data().lock);
while (simulation_running && sim.get_data().waiting_for_server)
sim.get_data().cv.wait(lock);
}
});
timer stopwatch;
unsigned long long elapsed = 0;
unsigned int frame_count = 0;
while (simulation_running) {
if (visualizer.is_window_closed())
break;
visualizer.draw_frame();
frame_count++;
if (stopwatch.milliseconds() >= 1000) {
elapsed += stopwatch.milliseconds();
printf("Completed %u moves: %lf simulation steps per second. (%lf fps)\n",
move_count, ((double) sim.time / elapsed) * 1000, ((double) frame_count / elapsed) * 1000);
stopwatch.start();
}
}
elapsed += stopwatch.milliseconds();
printf("Completed %u moves: %lf simulation steps per second. (%lf fps)\n", move_count, ((double) sim.time / elapsed) * 1000, ((double) frame_count / elapsed) * 1000);
std::unique_lock<std::mutex> lock(sim.get_data().lock);
simulation_running = false;
sim.get_data().cv.notify_one();
lock.unlock();
if (simulation_worker.joinable()) {
try {
simulation_worker.join();
} catch (...) { }
}
#if defined(RECORD)
free(collected_items);
#endif
return true;
}
bool run(
const char* server_address,
const char* server_port,
uint64_t track_agent_id,
float pixels_per_cell,
bool draw_scent_map,
bool draw_visual_field,
bool draw_agent_path,
float max_steps_per_second)
{
uint64_t client_id;
client<visualizer_client_data> sim;
simulation_running = true;
simulation_time = connect_client(sim, server_address, server_port, client_id);
if (simulation_time == UINT64_MAX) {
fprintf(stderr, "ERROR: Unable to connect to '%s:%s'.\n", server_address, server_port);
return false;
}
print_controls(stdout); fflush(stdout);
#if defined(RECORD)
log_file = (FILE*) fopen("recording.log", "wb");
#endif
bool fullscreen = false;
#if defined(RECORD)
fullscreen = true;
#endif
visualizer<client<visualizer_client_data>> visualizer(sim, 2560, 1440, track_agent_id,
pixels_per_cell, draw_scent_map, draw_visual_field, draw_agent_path, max_steps_per_second, fullscreen);
visualizer_running = &visualizer.running;
while (simulation_running && sim.client_running) {
if (visualizer.is_window_closed())
break;
visualizer.draw_frame();
}
#if defined(RECORD)
free(collected_items);
#endif
return remove_client(sim);
}
int main(int argc, const char** argv)
{
#if defined(WIN32)
SetConsoleCtrlHandler(signal_handler, TRUE);
#else
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#endif
if (argc <= 1) {
fprintf(stderr, "Not enough arguments.\n");
print_usage(stderr);
return EXIT_FAILURE;
}
uint64_t track_agent_id = 1;
char* server_address = nullptr;
const char* server_port = nullptr;
float pixels_per_cell = 6.0f;
float max_steps_per_second = 10.0f;
bool local = false;
bool draw_scent_map = true;
bool draw_visual_field = false;
bool draw_agent_path = false;
/* parse command-line arguments */
bool fail = false;
for (int i = 1; i < argc && !fail; i++) {
if (parse_address(argv[i], fail, server_address, server_port)) continue;
if (parse_option(argv[i], fail, "--track=", track_agent_id)) continue;
if (parse_option(argv[i], fail, "--pixels-per-cell=", pixels_per_cell)) continue;
if (parse_option(argv[i], fail, "--max-steps-per-sec=", max_steps_per_second)) continue;
if (parse_option(argv[i], fail, "--local")) { local = true; continue; }
if (parse_option(argv[i], fail, "--no-scent-map")) { draw_scent_map = false; continue; }
if (parse_option(argv[i], fail, "--visual-field")) { draw_visual_field = true; continue; }
if (parse_option(argv[i], fail, "--agent-path")) { draw_agent_path = true; continue; }
if (parse_option(argv[i], fail, "--help")) {
print_usage(stdout);
fflush(stdout);
if (server_address != nullptr)
free(server_address);
return EXIT_SUCCESS;
}
fprintf(stderr, "ERROR: Unrecognized command-line argument '%s'.\n", argv[i]);
fail = true;
}
if (pixels_per_cell <= 0.0f) {
fprintf(stderr, "ERROR: `pixels per cell` must be positive.\n");
fail = true;
}
if (fail) {
if (server_address != nullptr)
free(server_address);
return EXIT_FAILURE;
}
if (local) {
if (server_address != nullptr) {
free(server_address);
server_address = nullptr;
}
if (!run_locally(track_agent_id, pixels_per_cell, draw_scent_map, draw_visual_field, draw_agent_path, max_steps_per_second))
return EXIT_FAILURE;
} else {
if (server_address == nullptr || server_port == nullptr) {
fprintf(stderr, "ERROR: Address of JBW server not provided.\n");
return EXIT_FAILURE;
}
if (!run(server_address, server_port, track_agent_id, pixels_per_cell, draw_scent_map, draw_visual_field, draw_agent_path, max_steps_per_second)) {
free(server_address);
return EXIT_FAILURE;
}
}
#if defined(RECORD)
if (log_file != nullptr)
fclose(log_file);
#endif
if (server_address != nullptr)
free(server_address);
return EXIT_SUCCESS;
}