X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fengineer%2Fengineer.cpp;h=a6644abc0c04c58a58ef141492904f99c977d975;hb=d15ac13f2e170f155b4bbd124df48400c339b644;hp=573ef6f13ca7ad48637cbc5aed941fa29bdf08e8;hpb=6c61179fe09af2f5366d50f10aadbf5f83438087;p=r2c2.git diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 573ef6f..a6644ab 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -1,485 +1,462 @@ +#include #include +#include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libr2c2/driver.h" +#include "libr2c2/tracktype.h" +#include "3d/path.h" +#include "3d/track.h" +#include "3d/vehicle.h" #include "engineer.h" #include "mainpanel.h" #include "trainpanel.h" +#include "trainproperties.h" +#include "trainview.h" using namespace std; -using namespace Marklin; +using namespace R2C2; using namespace Msp; -#include +Application::RegApp Engineer::reg; Engineer::Engineer(int argc, char **argv): - screen_w(1280), - screen_h(960), - fullscreen(false), - layout(catalogue), + options(argc, argv), + window(options.screen_w, options.screen_h, options.fullscreen), + layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))), layout_3d(layout), - no_lighting(false), - placing_train(0) + server(0), + pipeline(window.get_width(), window.get_height(), false), + picking(false), + picking_track(0), + picking_entry(0), + picking_path(0), + pointer_x(0), + pointer_y(0), + pointer_moved(false) { - string res; - bool debug=false; - string device="/dev/ttyS0"; - unsigned quality=4; - - GetOpt getopt; - getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG); - getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG); - getopt.add_option('g', "debug", debug, GetOpt::NO_ARG); - getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG); - getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG); - getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG); - getopt(argc, argv); - - if(!res.empty()) + // Setup GUI + window.set_title("Railroad Engineer"); + window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0)); + + DataFile::load(ui_res, "r2c2.res"); + root = new GLtk::Root(ui_res, window); + root->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press)); + root->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion)); + root->set_visible(true); + + main_panel = new MainPanel(*this); + root->add(*main_panel); + main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h); + main_panel->set_visible(true); + + overlay = new Overlay3D(ui_res.get_default_font()); + + // Setup railroad control + DataFile::load(catalogue, "tracks.dat"); + DataFile::load(catalogue, "locos.dat"); + DataFile::load(catalogue, "wagons.dat"); + DataFile::load(layout, options.layout_fn); + + layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added)); + layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color))); + layout.signal_block_state_changed.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color))); + layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status)); + + if(FS::exists(options.state_fn)) + DataFile::load(layout, options.state_fn); + + if(options.network) { - if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res)) - { - screen_w=lexical_cast(m[1].str); - screen_h=lexical_cast(m[2].str); - } - else - throw UsageError("Invalid resolution"); + server = new Server(layout); + server->use_event_dispatcher(event_disp); } - if(device!="none") - control.open(device); + // Setup 3D view + DataFile::load(arrow_mesh, "arrow.mesh"); - control.set_debug(debug); + pipeline.set_camera(&camera); + pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0); + pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit"); + pipeline.add_renderable_for_pass(*overlay, "overlay"); - layout_3d.set_quality(quality); + light.set_position(GL::Vector4(0, -0.259, 0.966, 0)); + light.set_diffuse(GL::Color(0.9)); + lighting.set_ambient(GL::Color(0.4)); + lighting.attach(0, light); - catalogue.load("tracks.dat"); + GL::PipelinePass *pass = &pipeline.add_pass(0); + pass->depth_test = &GL::DepthTest::lequal(); + pass->lighting = &lighting; - const list &args=getopt.get_args(); - if(args.empty()) - throw UsageError("No layout given"); - layout.load(args.front()); + pass = &pipeline.add_pass("unlit"); + pass->depth_test = &GL::DepthTest::lequal(); - trfc_mgr=new TrafficManager(control, layout); + pass = &pipeline.add_pass("overlay"); + pass->blend = &GL::Blend::alpha(); view_all(); - /*const TrackSeq &tracks=layout.get_tracks(); - - for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - { - if(unsigned trnt_id=(*i)->get_turnout_id()) - { - Turnout *trnt=new Turnout(control, trnt_id); - trnt->signal_route_changed.connect(sigc::mem_fun(this, &Engineer::turnout_route_changed)); - } - if(unsigned sens_id=(*i)->get_sensor_id()) - { - Sensor *sens=new Sensor(control, sens_id); - sens->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_state_changed), sens_id)); - } - }*/ + // Catch various signals so we can stop the trains in case we get terminated + catch_signal(SIGINT); + catch_signal(SIGTERM); + catch_signal(SIGSEGV); + catch_signal(SIGILL); + catch_signal(SIGFPE); + catch_signal(SIGABRT); } Engineer::~Engineer() { -} + const map &trains = layout.get_trains(); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) + layout.get_driver().set_loco_speed(i->first, 0); + layout.get_driver().flush(); -void Engineer::add_train(unsigned addr) -{ - if(control.get_locomotive(addr)) - return; + if(!options.simulate) + { + layout.save_dynamic(options.state_fn+".tmp"); + FS::rename(options.state_fn+".tmp", options.state_fn); + } - Locomotive *loco=new Locomotive(control, addr); - Train *train=new Train(*trfc_mgr, *loco); + delete overlay; + delete root; - TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train); - int y=main_panel->get_geometry().y; - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - y-=(*i)->get_geometry().h; - tpanel->set_position(0, y-tpanel->get_geometry().h); - train_panels.push_back(tpanel); + delete server; +} - placing_train=train; +void Engineer::set_status(const string &text) +{ + main_panel->set_status_text(text); + status_timeout = Time::now()+10*Time::sec; } -int Engineer::main() +void Engineer::rearrange_panels() { - SDL_Init(SDL_INIT_VIDEO); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); - SDL_Surface *screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0)); - if(!screen) - { - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); - screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0)); - } - if(!screen) - { - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0); - screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0)); - } - if(!screen) - throw Exception("Couldn't create window"); - - glEnableClientState(GL_VERTEX_ARRAY); - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glDepthFunc(GL_LEQUAL); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - font=new GL::Font(); - if(screen_w>=1024) - { - font_size=20; - Parser::load(*font, "dejavu-20.font"); - } - else + int y = main_panel->get_geometry().y; + for(list::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) { - font_size=12; - Parser::load(*font, "dejavu-12.font"); + y -= (*i)->get_geometry().h; + (*i)->set_position(0, y); } +} - Parser::load(ui_res, "engineer.res"); - main_panel=new MainPanel(*this, ui_res); - main_panel->set_position(0, screen_h-main_panel->get_geometry().h); +void Engineer::add_train_view(TrainView &tv) +{ + train_views.push_back(&tv); +} - Application::main(); +void Engineer::remove_train_view(TrainView &tv) +{ + train_views.erase(remove(train_views.begin(), train_views.end(), &tv), train_views.end()); +} - delete font; - delete main_panel; +void Engineer::pick(bool with_ep) +{ + picking = true; + picking_track = 0; + picking_entry = (with_ep ? 0 : -1); +} - SDL_Quit(); +int Engineer::main() +{ + window.show(); - return exit_code; + return Application::main(); } void Engineer::tick() { - //cout<<"tick\n"; + window.get_display().tick(); - SDL_Event event; - while(SDL_PollEvent(&event)) - { - switch(event.type) - { - case SDL_MOUSEBUTTONDOWN: - button_press(event.button.x, screen_h-1-event.button.y, event.button.button); - break; - case SDL_MOUSEBUTTONUP: - button_release(event.button.x, screen_h-1-event.button.y, event.button.button); - break; - case SDL_MOUSEMOTION: - pointer_motion(event.motion.x, screen_h-1-event.motion.y); - break; - case SDL_KEYDOWN: - key_press(event.key.keysym.sym, event.key.keysym.mod); - break; - case SDL_QUIT: - exit(0); - break; - } - } + layout.tick(); + event_disp.tick(Time::zero); - control.tick(); + for(list::iterator i=train_views.begin(); i!=train_views.end(); ++i) + (*i)->prepare(); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); - - if(!no_lighting) + if(status_timeout && Time::now()>status_timeout) { - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - float params[4]; - params[0]=0; - params[1]=-0.2; - params[2]=1; - params[3]=0; - glLightfv(GL_LIGHT0, GL_POSITION, params); + main_panel->set_status_text(string()); + status_timeout = Time::TimeStamp(); } - //glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); + GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - layout_3d.render(); + pipeline.render_all(); - glDisable(GL_LIGHTING); - glColor4f(1, 1, 1, 1); - const Track3DSeq <racks=layout_3d.get_tracks(); - for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + if(pointer_moved) { - Track &track=(*i)->get_track(); - if(track.get_turnout_id()) + pointer_moved = false; + + if(picking) { - Turnout *trnt=control.get_turnout(track.get_turnout_id()); - if(trnt) - (*i)->render_route(trnt->get_route()); - else - (*i)->render_route(-1); + Track *track = pick_track(pointer_x, window.get_height()-pointer_y-1); + if(track && track!=picking_track) + { + picking_track = track; + if(picking_entry>=0) + picking_entry = 0; + + delete picking_path; + picking_path = new Path3D(layout_3d.get_track(*track)); + if(picking_entry>=0) + picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths); + else + picking_path->set_mask(picking_track->get_type().get_paths()); + picking_path->set_color(GL::Color(0)); + picking_path->set_layer(1); + } } - else - (*i)->render_route(-1); } - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, screen_w, 0, screen_h, 0, 1); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - glDisable(GL_MULTISAMPLE); + if(picking && picking_track && picking_entry>=0) + { + GL::PushMatrix push_mat; - main_panel->render(); - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - (*i)->render(); + float rot = picking_track->get_endpoint_direction(picking_entry); + Vector pos = picking_track->get_endpoint_position(picking_entry); - glLoadIdentity(); - glTranslatef(340, 10, 0); - glScalef(20, 20, 20); - glColor4f(1, 1, 1, 1); - font->draw_string(status_text); + GL::translate(pos.x, pos.y, pos.z+0.03); + GL::rotate(rot*180/M_PI+180, 0, 0, 1); - SDL_GL_SwapBuffers(); -} + arrow_mesh.draw(); + } -void Engineer::key_press(unsigned, unsigned) -{ -} + const GLtk::Geometry &rgeom = root->get_geometry(); + GL::matrix_mode(GL::PROJECTION); + GL::load_identity(); + GL::ortho_bottomleft(rgeom.w, rgeom.h); + GL::matrix_mode(GL::MODELVIEW); + GL::load_identity(); -void Engineer::button_press(int x, int y, unsigned btn) -{ - if(main_panel->get_geometry().is_inside(x, y)) { - main_panel->button_press(x, y, btn); - return; + GL::Bind blend(GL::Blend::alpha()); + root->render(); + GL::Texture::unbind(); } - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - if((*i)->get_geometry().is_inside(x, y)) - { - (*i)->button_press(x, y, btn); - return; - } - Track3D *track=pick_track(x, y); - if(track) + window.swap_buffers(); +} + +void Engineer::button_press(int x, int y, unsigned btn, unsigned) +{ + if(picking) { - if(placing_train) + if(btn==1 && picking_track) { - Section *sect=trfc_man->get_section_by_track(track->get_track()); + picking = false; + delete picking_path; + picking_path = 0; + signal_pick_done.emit(picking_track, picking_entry); } - else + else if(btn==3 && picking_entry>=0) { - Turnout *turnout=control.get_turnout(track->get_track().get_turnout_id()); - if(turnout) - turnout->set_route(1-turnout->get_route()); + picking_entry = (picking_entry+1)%picking_track->get_type().get_endpoints().size(); + picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths); } } -} - -void Engineer::button_release(int x, int y, unsigned btn) -{ - if(main_panel->get_geometry().is_inside(x, y)) + else { - main_panel->button_release(x, y, btn); - return; - } - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - if((*i)->get_geometry().is_inside(x, y)) + Track *track = pick_track(x, window.get_height()-y-1); + if(track) { - (*i)->button_release(x, y, btn); - return; + if(track->get_turnout_id()) + { + Block &block = track->get_block(); + if(block.get_train() && !block.get_train()->free_block(block)) + set_status("Turnout is busy"); + else + { + unsigned paths = track->get_type().get_paths(); + unsigned i = track->get_active_path()+1; + while(!(paths&(1<>i)) + i = 0; + else + ++i; + } + track->set_active_path(i); + set_status(format("Turnout %d", track->get_turnout_id())); + } + } + if(unsigned sid = track->get_sensor_id()) + { + if(options.simulate) + layout.get_driver().set_sensor(sid, !layout.get_driver().get_sensor(sid)); + set_status(format("Sensor %d", sid)); + } } + } } void Engineer::pointer_motion(int x, int y) { - if(main_panel->get_geometry().is_inside(x, y)) - { - main_panel->pointer_motion(x, y); - return; - } - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - if((*i)->get_geometry().is_inside(x, y)) - { - (*i)->pointer_motion(x, y); - return; - } - - Track3D *track=pick_track(x, y); - if(track && track->get_track().get_turnout_id()) - { - ostringstream ss; - ss<<"Turnout "<get_track().get_turnout_id(); - status_text=ss.str(); - } - else - status_text=""; + pointer_x = x; + pointer_y = y; + pointer_moved = true; } void Engineer::view_all() { - const Track3DSeq &tracks=layout_3d.get_tracks(); + const Layout3D::TrackMap &tracks = layout_3d.get_tracks(); - cam_rot=0; - float best_height=-1; - float mid_x=0; - float mid_y=0; + float view_aspect = float(window.get_width()-200)/window.get_height(); + float view_height = tan(camera.get_field_of_view()/2)*2; + float best_score = 0; + GL::Vector3 pos; + GL::Vector3 up; for(float angle=0; angleget_bounds(angle, minp, maxp); - min_x=min(min_x, minp.x); - max_x=max(max_x, maxp.x); - min_y=min(min_y, minp.y); - max_y=max(max_y, maxp.y); + Vector minp, maxp; + i->second->get_bounds(angle, minp, maxp); + min_x = min(min_x, minp.x); + max_x = max(max_x, maxp.x); + min_y = min(min_y, minp.y); + max_y = max(max_y, maxp.y); } - float width=max_x-min_x; - float height=max_y-min_y; - height=max(height, width); + float width = max_x-min_x; + float height = max_y-min_y; + float aspect = width/height; + float score = min(aspect/view_aspect, view_aspect/aspect); - if(heightbest_score) { - cam_rot=angle; - best_height=height; - mid_x=(min_x+max_x)/2; - mid_y=(min_y+max_y)/2; + best_score = score; + + float size = max(width/view_aspect, height); + float c = cos(angle); + float s = sin(angle); + float x = (min_x+max_x)/2-size*105/window.get_height(); + float y = (min_y+max_y)/2; + float z = max(size*1.05/view_height, 0.15); + + pos = GL::Vector3(c*x-s*y, s*x+c*y, z); + up = GL::Vector3(-s, c, 0); } } - float c=cos(cam_rot); - float s=sin(cam_rot); - cam_pos.x=c*mid_x-s*mid_y; - cam_pos.y=s*mid_x+c*mid_y; - cam_pos.z=max(best_height*1.05/0.82843, 0.15); + camera.set_position(pos); + camera.set_up_direction(up); + camera.set_look_direction(GL::Vector3(0, 0, -1)); + camera.set_aspect(float(window.get_width())/window.get_height()); + camera.set_depth_clip(pos.z*0.5, pos.z*1.5); } -void Engineer::turnout_route_changed(unsigned) +void Engineer::set_block_color(const Block &block, const GL::Color &color) { + const set &tracks = block.get_tracks(); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + layout_3d.get_track(**i).get_path().set_color(color); } -void Engineer::sensor_state_changed(bool state, unsigned addr) +void Engineer::reset_block_color(const Block &block) { - const Track3DSeq <racks=layout_3d.get_tracks(); - for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - if((*i)->get_track().get_sensor_id()==addr) - { - if(state) - (*i)->set_color(Color(1, 0, 0)); - else - (*i)->set_color(Color(1, 1, 1)); - } + bool active = block.get_state()>Block::INACTIVE; + + if(block.get_train()) + { + GL::Color color; + map::iterator i = train_colors.find(block.get_train()); + if(i!=train_colors.end()) + color = i->second; + + if(active) + set_block_color(block, color*0.6); + else + set_block_color(block, color*0.5+0.5); + } + else if(active) + set_block_color(block, GL::Color(0.6)); + else + set_block_color(block, GL::Color(1)); } -void Engineer::project_3d() +Track *Engineer::pick_track(int x, int y) { - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - //glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10); - glFrustum(-0.069036, 0.041421, -0.041421, 0.041421, 0.1, 10); - glMatrixMode(GL_MODELVIEW); + const GL::Vector3 &start = camera.get_position(); + float xx = x*2.0/window.get_width()-1.0; + float yy = y*2.0/window.get_height()-1.0; + GL::Vector4 ray = camera.unproject(GL::Vector4(xx, yy, 0, 0)); + + return layout.pick_track(Vector(start.x, start.y, start.z), Vector(ray.x, ray.y, ray.z)); } -Track3D *Engineer::pick_track(int x, int y) +void Engineer::train_added(Train &train) { - float xx=((float)(x-(int)screen_w*5/8)/screen_h)*0.82843; - //float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843; - float yy=((float)y/screen_h-0.5)*0.82843; - float size=(float)4/screen_h*0.82843; - - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); - - return layout_3d.pick_track(xx, yy, size); - - /*unsigned select_buf[1024]; - glSelectBuffer(1024, select_buf); - glRenderMode(GL_SELECT); - - //XXX Hardcoded values - float xn=((float)(x-800)/960)*0.082843; - float yn=((float)(y-480)/960)*0.082843; - float size=(float)4/960*0.082843; - - project_3d(); - glLoadIdentity(); - - double clip[4]; - clip[0]=0.1; - clip[1]=0; - clip[2]=xn-size; - clip[3]=0; - glClipPlane(GL_CLIP_PLANE0, clip); - glEnable(GL_CLIP_PLANE0); - - clip[0]=-0.1; - clip[2]=-(xn+size); - glClipPlane(GL_CLIP_PLANE1, clip); - glEnable(GL_CLIP_PLANE1); - - clip[0]=0; - clip[1]=0.1; - clip[2]=yn-size; - glClipPlane(GL_CLIP_PLANE2, clip); - glEnable(GL_CLIP_PLANE2); - - clip[1]=-0.1; - clip[2]=-(yn+size); - glClipPlane(GL_CLIP_PLANE3, clip); - glEnable(GL_CLIP_PLANE3); - - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); - - layout_3d.render(); - - glDisable(GL_CLIP_PLANE0); - glDisable(GL_CLIP_PLANE1); - glDisable(GL_CLIP_PLANE2); - glDisable(GL_CLIP_PLANE3); - - unsigned n_records=glRenderMode(GL_RENDER); - if(n_records) + TrainPanel *tpanel = new TrainPanel(*this, train); + root->add(*tpanel); + train_panels.push_back(tpanel); + rearrange_panels(); + + Vehicle3D &loco3d = layout_3d.get_vehicle(train.get_vehicle(0)); + overlay->set_label(loco3d, train.get_name()); + train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d))); + + GL::Color best_color; + float best_d_sq = 0; + for(unsigned i=0; i<10; ++i) { - Track *track=0; - unsigned i=0; - unsigned track_depth=numeric_limits::max(); - for(unsigned j=0; j::const_iterator j=train_colors.begin(); j!=train_colors.end(); ++j) { - unsigned ns_size=select_buf[i++]; - unsigned min_depth=select_buf[i++]; - ++i; // Skip max_depth - if(min_depthsecond.r; + float dg = color.g-j->second.g; + float db = color.b-j->second.b; + float d_sq = dr*dr+dg*dg+db*db; + if(d_sqbest_d_sq) + { + best_color = color; + best_d_sq = min_d_sq; } - - return track; } - - return 0;*/ + train_colors[&train] = best_color; } -Application::RegApp Engineer::reg; +void Engineer::sighandler(int sig) +{ + if(sig==SIGSEGV || sig==SIGILL || sig==SIGFPE || sig==SIGABRT) + { + signal(sig, SIG_DFL); + IO::print(IO::cerr, "Fatal signal received, terminating\n"); + const map &trains = layout.get_trains(); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) + layout.get_driver().set_loco_speed(i->first, 0); + layout.get_driver().flush(); + raise(sig); + } + else if(sig==SIGTERM || sig==SIGINT) + exit(0); +}