3 This file is part of R²C²
4 Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
16 TrackType::TrackType(const ArticleNumber &an):
22 float TrackType::get_total_length() const
24 return get_path_length(-1);
27 float TrackType::get_path_length(int p) const
30 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
31 if(p<0 || i->get_path()==static_cast<unsigned>(p))
32 len += i->get_length();
36 unsigned TrackType::get_paths() const
39 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
40 mask |= 1<<i->get_path();
44 unsigned TrackType::get_n_paths() const
47 for(unsigned mask = get_paths(); mask; ++n)
52 bool TrackType::is_turnout() const
54 return endpoints.size()>2;
57 bool TrackType::is_dead_end() const
59 return endpoints.size()<2;
62 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
64 if(i>=endpoints.size())
65 throw InvalidParameterValue("Endpoint index out of range");
70 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
72 if(epi>=endpoints.size())
73 throw InvalidParameterValue("Endpoint index out of range");
75 const TrackPart *part = 0;
77 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
79 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
82 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
83 for(unsigned j=0; j<n_part_eps; ++j)
85 TrackPoint p = i->get_point(j ? i->get_length() : 0);
86 float dx = p.pos.x-endpoints[epi].pos.x;
87 float dy = p.pos.y-endpoints[epi].pos.y;
97 throw Exception("Internal error (endpoint does not match any part)");
101 float plen = part->get_length();
106 TrackPoint p = part->get_point(d);
114 TrackPart *next = part->get_link(1-part_ep);
116 throw InvalidParameterValue("Distance out of range");
117 part_ep = (next->get_link(0)==part ? 0 : 1);
123 bool TrackType::collide_ray(const Vector &start, const Vector &dir, float width) const
125 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
126 if(i->collide_ray(start, dir, width))
132 void TrackType::collect_endpoints()
136 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
138 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
141 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
142 for(unsigned j=0; j<n_part_eps; ++j)
145 TrackPoint p = i->get_point(j ? i->get_length() : 0);
150 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
152 float dx = k->pos.x-p.pos.x;
153 float dy = k->pos.y-p.pos.y;
155 float da = k->dir-p.dir;
161 if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
163 k->paths |= 1<<i->get_path();
170 endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
175 TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
182 TrackType::Loader::Loader(TrackType &t):
183 Msp::DataFile::BasicLoader<TrackType>(t),
184 state_bits_set(false)
186 add("autofit_preference", &TrackType::autofit_preference);
187 add("description", &TrackType::description);
188 add("object", &TrackType::object);
189 add("state_bits", &Loader::state_bits);
190 add("part", &Loader::part);
193 void TrackType::Loader::finish()
195 obj.collect_endpoints();
198 void TrackType::Loader::part()
202 obj.parts.push_back(p);
203 if(!state_bits_set && p.get_path())
204 while(p.get_path()>=(1U<<obj.state_bits))
208 void TrackType::Loader::state_bits(unsigned b)
211 state_bits_set = true;