Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ turns the sky around the scene. The page also takes `?time=HH:MM` and `?day-leng

The native viewer takes `--weather NAME` or a weather file, and **C** moves to the next kind of
weather. `--time HH:MM` starts a clock, with `--day N` (day of the year), `--latitude DEGREES` and
`--day-length SECONDS`; **T** moves the clock on an hour.
`--day-length SECONDS`; **T** moves the clock on an hour. While a game is played, C and T are the
game's keys, as **C** changes a car's view; they are the viewer's again while flying (**V**).

## Your own models

Expand Down
11 changes: 9 additions & 2 deletions apps/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,18 +1655,25 @@ mod native {
}
}
}
// While someone is played, and not left to fly the camera, the keys
// are the game's: C changes a car's view, so it is not the weather's.
let played = self
.state
.as_ref()
.and_then(|s| s.play.as_ref())
.is_some_and(|(play, _)| play.play.has_player() && !play.play.flying());
match event.state {
ElementState::Pressed => {
self.keys.insert(code);
if code == KeyCode::Escape {
self.set_mouse_capture(false);
}
// Cycles through the built-in weather, easing into each.
if code == KeyCode::KeyC {
if code == KeyCode::KeyC && !played {
self.next_weather();
}
// Moves the clock on an hour, starting one if there is none.
if code == KeyCode::KeyT {
if code == KeyCode::KeyT && !played {
let fixed = self.options.environment;
let clock = self.options.clock.get_or_insert_with(|| {
let mut clock = DayClock::new(TimeOfDay::default(), 0.0);
Expand Down
15 changes: 14 additions & 1 deletion crates/solstice-game/src/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ impl Play {
let circuit = track.clone();
// Whether the car is driving itself round the circuit, and where on it it last was.
let (mut itself, mut last_at) = (false, None);
// Whether the player's own gearbox is automatic, while the car drives itself.
let mut players_gearbox = true;
schedule.add(TickGroup::PrePhysics, "driving", move |world| {
let Some(entity) = driven else {
return;
Expand All @@ -363,9 +365,20 @@ impl Play {
drive::asked(world.input())
};
let reset = !away && world.input().pressed("reset");
if !away && world.input().pressed("autopilot") {
// With no circuit there is nothing to drive round: the key does nothing.
if !away && circuit.is_some() && world.input().pressed("autopilot") {
itself = !itself;
last_at = None;
// The driver it is handed to changes gear itself, whatever gearbox the
// player had; handed back, the player has their own again.
if let Ok(mut car) = world.entities.get::<&mut Car>(entity) {
if itself {
players_gearbox = car.driver.automatic;
car.driver.automatic = true;
} else {
car.driver.automatic = players_gearbox;
}
}
}
if let (true, Some(track)) = (itself, &circuit) {
let car = world
Expand Down
27 changes: 26 additions & 1 deletion crates/solstice-game/src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,19 @@ impl Track {
let mut name = String::new();
let mut samples = Vec::new();
let mut sectors = Vec::new();
let mut named = None;
for (line, words) in lines(text) {
let error = |message: String| FileError { line, message };
match words[0] {
"name" if words.len() >= 2 => name = words[1..].join(" "),
"name" if words.len() >= 2 => {
if let Some(first) = named {
return Err(error(format!(
"a second name: the first is on line {first}"
)));
}
named = Some(line);
name = words[1..].join(" ");
}
"name" => return Err(error("expected `name NAME`".to_owned())),
"sector" => {
let [_, index] = words[..] else {
Expand Down Expand Up @@ -180,6 +189,11 @@ impl Track {
Some(("r", bands)) => (&mut sample.right_bands, bands),
_ => return Err(error(format!("`{word}` should be l=... or r=..."))),
};
if !side.is_empty() {
return Err(error(format!(
"`{word}`: a side's bands are given once, as l=A,B"
)));
}
for band in bands.split(',') {
side.push(parse_band(band).map_err(&error)?);
}
Expand Down Expand Up @@ -537,6 +551,17 @@ pub(crate) mod tests {
("p 0 0 0 1 0 0 6 6 x=kerb:3", 1, "l=... or r=..."),
("p 0 0 0 1 0 0 6 6 l=kerb", 1, "SURFACE:WIDTH"),
("lap 3", 1, "unknown line `lap`"),
(
"name A
name B",
2,
"a second name: the first is on line 1",
),
(
"p 0 0 0 1 0 0 6 6 l=kerb:1 l=grass:3",
1,
"a side's bands are given once",
),
(
"p 0 0 0 1 0 0 6 6\np 1 0 0 1 0 0 6 6",
1,
Expand Down
49 changes: 49 additions & 0 deletions crates/solstice-game/tests/driving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,55 @@ actor car.actor -200 0 30 0 instances=1+6
assert!(car_at(&play).1 < before, "coasting");
}

#[test]
fn a_manual_car_driving_itself_changes_gear_and_the_player_gets_their_gearbox_back() {
let track_text = square_track();
let scene = "input keys.input
track square.track
actor car.actor -200 0 30 0 instances=1+6
";
let manual = CAR.replace("vehicle gt.vehicle", "vehicle gt.vehicle manual");
let mut play = Play::load(
&files(&[
(GAME_FILE, scene),
("car.actor", &manual),
("gt.vehicle", ""),
("keys.input", KEYS),
("square.track", &track_text),
]),
|id| baked(id).map(|at| Mat4::from_translation(Vec3::new(-200.0, 0.0, 30.0)) * at),
Some(&floor()),
)
.unwrap_or_else(|e| panic!("{e:?}"));
let gearbox = |play: &Play| {
let world = &play.world().entities;
let mut query = world.query::<&Car>();
let car = query.iter().next().expect("a car");
(car.driver.automatic, car.vehicle.gear())
};
let mut devices = Devices::new();
run(&mut play, &mut devices, 0.5);
assert!(!gearbox(&play).0, "the player's car is manual");
devices.key("KeyP", true);
run(&mut play, &mut devices, 0.1);
devices.key("KeyP", false);
let mut highest = 0;
for _ in 0..(12.0 / STEP_SECONDS) as u32 {
play.frame(STEP_SECONDS, &mut devices, |_, _| {});
highest = highest.max(gearbox(&play).1);
}
assert!(gearbox(&play).0, "its own driver changes gear");
assert!(highest >= 3, "it got no higher than gear {highest} in 12 s");
// Handed back, the gearbox is the player's again, and stays in the gear it was in.
devices.key("KeyP", true);
run(&mut play, &mut devices, 0.1);
devices.key("KeyP", false);
let (automatic, now) = gearbox(&play);
assert!(!automatic);
run(&mut play, &mut devices, 2.0);
assert_eq!(gearbox(&play).1, now, "nothing changes gear for the player");
}

#[test]
fn a_car_that_falls_far_below_its_track_is_put_back_on_it() {
// The square circuit forty metres up, over a floor at nought: the car is put down beside
Expand Down
46 changes: 46 additions & 0 deletions crates/solstice-vehicle/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use crate::tyre::Tyre;
use glam::Vec3;
use std::collections::HashMap;
use std::fmt::{self, Write as _};

/// A mistake in a `.vehicle` file, and the line it is on.
Expand Down Expand Up @@ -348,6 +349,8 @@ impl VehicleSpec {
pub fn parse(text: &str) -> Result<Self, SpecError> {
let mut spec = Self::gt();
let mut torque: Vec<(f32, f32)> = Vec::new();
// Each setting, and the line it was given on: a second would silently win.
let mut given: HashMap<String, usize> = HashMap::new();
for (index, line) in text.lines().enumerate() {
let words: Vec<&str> = line
.split('#')
Expand All @@ -368,6 +371,25 @@ impl VehicleSpec {
}
values.iter().map(|w| number(w).map_err(&error)).collect()
};
let settings: Vec<String> = match keyword {
// A line for each point of the curve.
"torque" => Vec::new(),
"front" | "rear" | "tyre" | "engine" | "gearbox" | "diff" | "aero" | "steering"
| "geometry" | "assists" => values
.iter()
.filter_map(|word| word.split_once('='))
.map(|(key, _)| format!("{keyword} {key}"))
.collect(),
_ => vec![keyword.to_owned()],
};
for setting in settings {
if let Some(first) = given.get(&setting) {
return Err(error(format!(
"`{setting}` is given twice: first on line {first}"
)));
}
given.insert(setting, index + 1);
}
match keyword {
"mass" => spec.mass = numbers(1, "KILOGRAMS")?[0],
"inertia" => spec.inertia = Vec3::from_slice(&numbers(3, "PITCH YAW ROLL")?),
Expand Down Expand Up @@ -987,6 +1009,30 @@ mod tests {
("body 1 1 1", 1, "body SX SY SZ at"),
("tyre along=2.5,0", 1, "C between 1 and 2"),
("aero balance=1.4", 1, "share"),
(
"mass 1200
mass 1300",
2,
"`mass` is given twice: first on line 1",
),
(
"gears 3 2

gears 3 2 1",
3,
"`gears` is given twice",
),
(
"front spring=1
front spring=2",
2,
"`front spring` is given twice",
),
(
"engine idle=900 idle=950",
1,
"`engine idle` is given twice",
),
];
for (text, line, part) in cases {
let error = VehicleSpec::parse(text).unwrap_err();
Expand Down
8 changes: 5 additions & 3 deletions docs/gameplay.md
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,9 @@ Built for a racing game on the engine,
- Anti-lock brakes, traction control and stability control, each a line in the car's file.
Stability control watches the rear tyres' slip angle: past the angle they grip best at,
it eases the engine, lets go of the rear brakes and brakes the outside front wheel.
- A `.vehicle` file holds a car's numbers. `tests/gt.vehicle` is a rear-engined GT racing
car, and its tests hold it to what its class does (`tests/rear_engined.rs`).
- A `.vehicle` file holds a car's numbers, each given once. `tests/gt.vehicle` is a
rear-engined GT racing car, and its tests hold it to what its class does
(`tests/rear_engined.rs`).
- **The driver** (`driver.rs`) is between the player and the car, as in every driving game.
A key is a wheel hard over; a wheel hard over at speed is a spin. So steering eases, a full
push gives less lock the faster the car goes, and past a few degrees of slide the wheel
Expand All @@ -1093,7 +1094,8 @@ Built for a racing game on the engine,
actors; a scene without one leaves them out, so its keys press nothing more. They are
`throttle`, `brake`, `steer`, `handbrake`, `shift_up`, `shift_down`, `reset`, `camera`,
`look_back`, `look_round`, `look_over`, and `autopilot`, which hands the car to a driver of its own that follows the
track's centre line at a speed the bends ahead allow. That is for showing a circuit off,
track's centre line at a speed the bends ahead allow, changing gear itself even for a
`manual` car (the player has their own gearbox back when they take over). That is for showing a circuit off,
and for trying one headless: `viewer SCENE --play --press autopilot@40 --frames 5000`.
- **Building one.** `circuit SIZE` in a scene file builds a whole circuit from the scene's
track (`apps/bake/src/circuit`): an island whose height follows the road, with cliffs, the
Expand Down
Loading