Skyscraper 2.0
vehicles.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.0 Alpha - Script Processor - Vehicle Section
3 Copyright (C)2003-2024 Ryan Thoryk
4 https://www.skyscrapersim.net
5 https://sourceforge.net/projects/skyscraper/
6 Contact - ryan@skyscrapersim.net
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21*/
22
23#include "globals.h"
24#include "sbs.h"
25#include "enginecontext.h"
26#include "vehicle.h"
27#include "scriptproc.h"
28#include "section.h"
29
30using namespace SBS;
31
32namespace Skyscraper {
33
38
40{
41 //Process vehicles
42
43 //create vehicle if not created already
45
46 //replace variables with actual values
47 ReplaceAll(LineData, "%vehicle%", ToString(config->Current));
48
49 //IF/While statement stub (continue to global commands for processing)
50 if (StartsWithNoCase(LineData, "if") || StartsWithNoCase(LineData, "while"))
51 return sContinue;
52
53 //process math functions
54 if (MathFunctions(LineData) == sError)
55 return sError;
56
57 //process functions
58 if (parent->FunctionProc() == true)
59 return sNextLine;
60
61 //get text after equal sign
62 bool equals;
63 std::string value = GetAfterEquals(LineData, equals);
64
65 //get vehicle object
67
68 //parameters
69
70 //Name parameter
71 if (StartsWithNoCase(LineData, "name"))
72 {
73 if (equals == false)
74 return ScriptError("Syntax error");
75 v->Name = value;
76 return sNextLine;
77 }
78 //MaxEngineForce parameter
79 if (StartsWithNoCase(LineData, "maxengineforce"))
80 {
81 if (equals == false)
82 return ScriptError("Syntax error");
83 std::string str = Calc(value);
84 if (!IsNumeric(str, v->MaxEngineForce))
85 return ScriptError("Invalid value");
86 return sNextLine;
87 }
88 //SteeringIncrement parameter
89 if (StartsWithNoCase(LineData, "steeringincrement"))
90 {
91 if (equals == false)
92 return ScriptError("Syntax error");
93 std::string str = Calc(value);
94 if (!IsNumeric(str, v->SteeringIncrement))
95 return ScriptError("Invalid value");
96 return sNextLine;
97 }
98 //SteeringClamp parameter
99 if (StartsWithNoCase(LineData, "steeringclamp"))
100 {
101 if (equals == false)
102 return ScriptError("Syntax error");
103 std::string str = Calc(value);
104 if (!IsNumeric(str, v->SteeringClamp))
105 return ScriptError("Invalid value");
106 return sNextLine;
107 }
108 //WheelFriction parameter
109 if (StartsWithNoCase(LineData, "wheelfriction"))
110 {
111 if (equals == false)
112 return ScriptError("Syntax error");
113 std::string str = Calc(value);
114 if (!IsNumeric(str, v->WheelFriction))
115 return ScriptError("Invalid value");
116 return sNextLine;
117 }
118 //SuspensionStiffness parameter
119 if (StartsWithNoCase(LineData, "suspensionstiffness"))
120 {
121 if (equals == false)
122 return ScriptError("Syntax error");
123 std::string str = Calc(value);
124 if (!IsNumeric(str, v->SuspensionStiffness))
125 return ScriptError("Invalid value");
126 return sNextLine;
127 }
128 //SuspensionDamping parameter
129 if (StartsWithNoCase(LineData, "suspensiondamping"))
130 {
131 if (equals == false)
132 return ScriptError("Syntax error");
133 std::string str = Calc(value);
134 if (!IsNumeric(str, v->SuspensionDamping))
135 return ScriptError("Invalid value");
136 return sNextLine;
137 }
138 //SuspensionCompression parameter
139 if (StartsWithNoCase(LineData, "suspensioncompression"))
140 {
141 if (equals == false)
142 return ScriptError("Syntax error");
143 std::string str = Calc(value);
144 if (!IsNumeric(str, v->SuspensionCompression))
145 return ScriptError("Invalid value");
146 return sNextLine;
147 }
148 //RollInfluence parameter
149 if (StartsWithNoCase(LineData, "rollinfluence"))
150 {
151 if (equals == false)
152 return ScriptError("Syntax error");
153 std::string str = Calc(value);
154 if (!IsNumeric(str, v->RollInfluence))
155 return ScriptError("Invalid value");
156 return sNextLine;
157 }
158 //SuspensionRestLength parameter
159 if (StartsWithNoCase(LineData, "suspensionrestlength"))
160 {
161 if (equals == false)
162 return ScriptError("Syntax error");
163 std::string str = Calc(value);
164 if (!IsNumeric(str, v->SuspensionRestLength))
165 return ScriptError("Invalid value");
166 return sNextLine;
167 }
168 //MaxSuspensionTravelCm parameter
169 if (StartsWithNoCase(LineData, "maxsuspensiontravelcm"))
170 {
171 if (equals == false)
172 return ScriptError("Syntax error");
173 std::string str = Calc(value);
174 if (!IsNumeric(str, v->MaxSuspensionTravelCm))
175 return ScriptError("Invalid value");
176 return sNextLine;
177 }
178 //FrictionSlip parameter
179 if (StartsWithNoCase(LineData, "frictionslip"))
180 {
181 if (equals == false)
182 return ScriptError("Syntax error");
183 std::string str = Calc(value);
184 if (!IsNumeric(str, v->FrictionSlip))
185 return ScriptError("Invalid value");
186 return sNextLine;
187 }
188 //ChassisShift parameter
189 if (StartsWithNoCase(LineData, "chassisshift"))
190 {
191 int params = SplitAfterEquals(LineData);
192 if (params != 3)
193 return ScriptError("Incorrect number of parameters");
194
195 //check numeric values
196 for (int i = 0; i <= 2; i++)
197 {
198 if (!IsNumeric(tempdata[i]))
199 return ScriptError("Invalid value: " + tempdata[i]);
200 }
201
202 v->ChassisShift = Vector3(ToFloat(tempdata[0]), ToFloat(tempdata[1]), ToFloat(tempdata[2]));
203 return sNextLine;
204 }
205 //ChassisMesh parameter
206 if (StartsWithNoCase(LineData, "chassismesh"))
207 {
208 if (equals == false)
209 return ScriptError("Syntax error");
210 v->ChassisMesh = value;
211 return sNextLine;
212 }
213 //WheelMesh parameter
214 if (StartsWithNoCase(LineData, "wheelmesh"))
215 {
216 if (equals == false)
217 return ScriptError("Syntax error");
218 v->WheelMesh = value;
219 return sNextLine;
220 }
221 //ChassisScale parameter
222 if (StartsWithNoCase(LineData, "chassisscale"))
223 {
224 if (equals == false)
225 return ScriptError("Syntax error");
226 std::string str = Calc(value);
227 if (!IsNumeric(str, v->ChassisScale))
228 return ScriptError("Invalid value");
229 return sNextLine;
230 }
231 //WheelScale parameter
232 if (StartsWithNoCase(LineData, "wheelscale"))
233 {
234 if (equals == false)
235 return ScriptError("Syntax error");
236 std::string str = Calc(value);
237 if (!IsNumeric(str, v->WheelScale))
238 return ScriptError("Invalid value");
239 return sNextLine;
240 }
241
242 //CreateVehicle command
243 if (StartsWithNoCase(LineData, "createvehicle"))
244 {
245 //get data
246 int params = SplitData(LineData, 14);
247
248 if (params != 3)
249 return ScriptError("Incorrect number of parameters");
250
251 //check numeric values
252 for (int i = 0; i <= 2; i++)
253 {
254 if (!IsNumeric(tempdata[i]))
255 return ScriptError("Invalid value: " + tempdata[i]);
256 }
257
258 Vector3 position = Vector3(ToFloat(tempdata[0]), ToFloat(tempdata[1]), ToInt(tempdata[2]));
259 bool result = v->Create(position);
260 if (result == false)
261 return ScriptError();
262 StoreCommand(v);
263 return sNextLine;
264 }
265
266 //CreateChassis command
267 if (StartsWithNoCase(LineData, "createchassis"))
268 {
269 //get data
270 int params = SplitData(LineData, 14);
271
272 if (params != 5)
273 return ScriptError("Incorrect number of parameters");
274
275 //check numeric values
276 for (int i = 0; i <= 4; i++)
277 {
278 if (!IsNumeric(tempdata[i]))
279 return ScriptError("Invalid value: " + tempdata[i]);
280 }
281
282 bool result = v->CreateChassis(ToFloat(tempdata[0]), ToFloat(tempdata[1]), ToFloat(tempdata[2]), ToFloat(tempdata[3]), ToFloat(tempdata[4]));
283 if (result == false)
284 return ScriptError();
285 return sNextLine;
286 }
287
288 //AddWheel command
289 if (StartsWithNoCase(LineData, "addwheel"))
290 {
291 //get data
292 int params = SplitData(LineData, 9);
293
294 if (params != 7 && params != 13)
295 return ScriptError("Incorrect number of parameters");
296
297 //check numeric values
298 for (int i = 3; i < params; i++)
299 {
300 if (!IsNumeric(tempdata[i]))
301 return ScriptError("Invalid value: " + tempdata[i]);
302 }
303
304 Vector3 connection = Vector3(ToFloat(tempdata[4]), ToFloat(tempdata[5]), ToFloat(tempdata[6]));
305 Vector3 direction, axle;
306 if (params > 7)
307 {
308 direction = Vector3(ToFloat(tempdata[7]), ToFloat(tempdata[8]), ToFloat(tempdata[9]));
309 axle = Vector3(ToFloat(tempdata[10]), ToFloat(tempdata[11]), ToFloat(tempdata[12]));
310 }
311 bool result;
312 if (params == 7)
313 result = v->AddWheel(ToBool(tempdata[0]), ToBool(tempdata[1]), ToBool(tempdata[2]), ToFloat(tempdata[3]), connection);
314 else
315 result = v->AddWheel(ToBool(tempdata[0]), ToBool(tempdata[1]), ToBool(tempdata[2]), ToFloat(tempdata[3]), connection, direction, axle);
316
317 if (result == false)
318 return ScriptError();
319 return sNextLine;
320 }
321
322 //handle end of vehicle section
323 if (StartsWithNoCase(LineData, "<endvehicle>") && config->RangeL == config->RangeH)
324 {
325 config->SectionNum = 0;
326 config->Context = "None";
327 engine->Report("Finished vehicle");
328 return sNextLine;
329 }
330
331 //handle vehicle range
332 if (config->RangeL != config->RangeH && StartsWithNoCase(LineData, "<endvehicle"))
333 {
334 if (config->Current < config->RangeH)
335 {
336 config->Current++;
337 parent->line = config->RangeStart; //loop back
338 return sNextLine;
339 }
340 else
341 {
342 config->SectionNum = 0; //break out of loop
343 config->Context = "None";
344 config->RangeL = 0;
345 config->RangeH = 0;
346 engine->Report("Finished vehicles");
347 return sNextLine;
348 }
349 }
350
351 return sContinue;
352}
353
354}
Vehicle * NewVehicle(int number)
Definition sbs.cpp:1689
Vehicle * GetVehicle(int number)
Definition sbs.cpp:1767
Real ChassisScale
Definition vehicle.h:46
Real SuspensionStiffness
Definition vehicle.h:54
bool CreateChassis(Real restitution, Real friction, Real mass, Real linear_dampening, Real angular_dampening)
Definition vehicle.cpp:159
Real FrictionSlip
Definition vehicle.h:60
Real WheelFriction
Definition vehicle.h:53
std::string WheelMesh
Definition vehicle.h:45
Real SuspensionRestLength
Definition vehicle.h:58
Real WheelScale
Definition vehicle.h:47
Real SuspensionDamping
Definition vehicle.h:55
std::string ChassisMesh
Definition vehicle.h:44
Real MaxSuspensionTravelCm
Definition vehicle.h:59
Real SuspensionCompression
Definition vehicle.h:56
Real RollInfluence
Definition vehicle.h:57
Real MaxEngineForce
Definition vehicle.h:50
bool AddWheel(bool engine, bool steerable, bool IsFrontWheel, Real radius, const Vector3 &ConnectionPoint, const Vector3 &Direction=Vector3(0, -1, 0), const Vector3 &Axle=Vector3(-1, 0, 0))
Definition vehicle.cpp:207
bool Create(const Vector3 &position)
Definition vehicle.cpp:144
Real SteeringIncrement
Definition vehicle.h:51
std::string Name
Definition vehicle.h:40
Real SteeringClamp
Definition vehicle.h:52
Vector3 ChassisShift
Definition vehicle.h:43
void Report(const std::string &message)
VehicleSection(ScriptProcessor *parent)
Definition vehicles.cpp:34
static const int sNextLine
Definition scriptproc.h:70
static const int sContinue
Definition scriptproc.h:69
static const int sError
Definition scriptproc.h:71
void StoreCommand(::SBS::Object *object)
std::string Calc(const std::string &expression)
Ogre::Vector3 Vector3
Definition globals.h:58
bool StartsWithNoCase(const std::string &string, const std::string &check_string)
Definition globals.cpp:237
void ReplaceAll(std::string &string, const std::string &original, const std::string &replacement)
Definition globals.cpp:201
int ToInt(const std::string &string)
Definition globals.cpp:402
std::string ToString(int number)
Definition globals.cpp:279
Real ToFloat(const std::string &string)
Definition globals.cpp:397
bool ToBool(std::string string)
Definition globals.cpp:407
bool IsNumeric(const wxString &string)