[go: up one dir, main page]

Menu

[f338fb]: / vamos / vamos.cc  Maximize  Restore  History

Download this file

219 lines (188 with data), 6.7 kB

  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
// Copyright (C) 2001--2014 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#include <boost/filesystem.hpp>
#include "../body/Gl_Car.h"
#include "../media/Ac3d.h"
#include "../track/Strip_Track.h"
#include "../world/Atmosphere.h"
#include "../world/Gl_World.h"
#include "../world/Sounds.h"
#include "../world/Interactive_Driver.h"
#include "../world/Robot_Driver.h"
#include "Options.h"
using namespace Vamos_Geometry;
using namespace Vamos_Media;
using namespace Vamos_Track;
using namespace Vamos_World;
namespace fs = boost::filesystem;
struct Entry
{
Entry (const std::string& file_in, bool interactive_in, double time_in)
: file (file_in),
interactive (interactive_in),
time (time_in)
{}
std::string file;
bool interactive;
double time;
static bool quicker (const Entry& e1, const Entry& e2) { return (e1.time < e2.time); }
};
typedef std::vector <Entry> Entry_List;
std::string
get_path (std::string file, std::string section, bool extend)
{
std::string path ("../data/" + section + "/" + file + (extend ? ".xml" : ""));
if (fs::exists (path))
return path;
path = DATADIR "/" + section + "/" + file + (extend ? ".xml" : "");
if (fs::exists (path))
return path;
return file;
}
void read_input (Options& opt, Entry_List& entries)
{
std::ifstream in (opt.input_file.c_str ());
in >> opt.track_file;
int lap;
double time;
in >> lap >> lap >> time;
while (true)
{
std::string file, name, type;
in >> file >> name >> type >> lap >> time;
if (!in)
break;
entries.push_back (Entry (file, type == "interactive", time));
}
std::sort (entries.begin (), entries.end (), Entry::quicker);
opt.number_of_cars = entries.size ();
}
void get_entries (Options& opt, Entry_List& entries)
{
if (!opt.input_file.empty ())
return read_input (opt, entries);
opt.track_file = get_path (opt.track_file, "tracks", true);
std::vector <std::string> car_files;
fs::path car_path (get_path (opt.car_file, "cars", false));
if (fs::is_directory (car_path))
{
for (fs::directory_iterator it = fs::directory_iterator (car_path);
(it != fs::directory_iterator ())
&& (car_files.size () < opt.number_of_cars);
it++)
{
if (it->path ().extension () == ".xml")
car_files.push_back (it->path ().native ());
}
std::sort (car_files.begin (), car_files.end ());
}
else
car_files.push_back (get_path (opt.car_file, "cars", true));
std::sort (car_files.begin (), car_files.end ());
std::vector <std::string>::const_iterator it = car_files.begin ();
while (entries.size () < opt.number_of_cars)
{
entries.push_back (Entry (*it, false, 0));
if (++it == car_files.end ())
it = car_files.begin ();
}
if (!opt.demo)
entries.back ().interactive = true;
}
int main (int argc, char* argv [])
{
Options opt (argc, argv);
if (!opt)
std::exit (opt.exit_status ());
Strip_Track track;
Atmosphere air (1.2, Three_Vector (0.0, 0.0, 0.0));
Sounds sounds (opt.volume);
Gl_World world (track, air, sounds, opt.full_screen);
try
{
Entry_List entries;
get_entries (opt, entries);
track.read (opt.data_dir, opt.track_file);
if (!opt.map_mode)
{
bool robots = false;
bool interactive = false;
Three_Matrix orientation;
for (Entry_List::const_iterator itEntry = entries.begin ();
itEntry != entries.end ();
++itEntry)
{
const int place = itEntry - entries.begin () + 1;
Vamos_Body::Gl_Car* car
= new Vamos_Body::Gl_Car (track.grid_position (place,
opt.number_of_cars,
opt.qualifying),
orientation);
car->read (opt.data_dir, itEntry->file);
car->start_engine ();
if (itEntry->interactive)
{
interactive = true;
Interactive_Driver* driver = new Interactive_Driver (*car);
world.add_car (*car, *driver);
world.set_focused_car (place - 1);
}
else
{
robots = true;
car->adjust_robot_parameters (opt.performance,
opt.performance,
opt.performance);
Robot_Driver* driver
= new Robot_Driver (*car, track, world.get_gravity ());
if (opt.qualifying)
driver->qualify ();
driver->interact (opt.interact);
driver->show_steering_target (opt.show_line);
world.add_car (*car, *driver);
}
}
if (robots && !interactive)
world.set_focused_car (0);
world.cars_can_interact (opt.interact);
}
world.read (get_path (opt.world_file, "worlds", true),
get_path (opt.controls_file, "controls", true));
if (!opt.map_mode)
sounds.read (opt.data_dir + "sounds/", "default-sounds.xml");
}
catch (XML_Exception& error)
{
std::cerr << error.message () << std::endl;
std::exit (EXIT_FAILURE);
}
catch (Vamos_Media::Ac3d_Exception& error)
{
std::cerr << error.message () << std::endl;
std::exit (EXIT_FAILURE);
}
if (opt.show_line || opt.demo || (opt.number_of_cars > 1))
{
track.build_racing_line ();
track.show_racing_line (opt.show_line);
}
world.start (opt.qualifying, opt.laps);
std::ostringstream name;
name << (opt.qualifying ? "qualifying" : "race") << "-results";
world.write_results (name.str ());
return EXIT_SUCCESS;
}