Skip to content

Commit

Permalink
Convert uses of PlaneType within server to instead use plane protot…
Browse files Browse the repository at this point in the history
…ypes (#198)

This PR converts pretty much all uses of `PlaneType` and `Config::planes` to instead use plane prototypes. Along with that, it also converts a bunch of missile-related code to use missile prototypes.

It passes all tests, although due to the fact that there are multiple different configs it breaks custom configs. This will get fixed once the conversion is completed and the new config system is being used.
  • Loading branch information
steamroller-airmash committed Jun 18, 2022
1 parent ab0e01e commit 48bf0e3
Show file tree
Hide file tree
Showing 32 changed files with 406 additions and 283 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde-deserialize-over = "0.1.1"

airmash-protocol = { version = "0.5.0", features = ["serde"] }
server-macros = { path="../server-macros" }
server-config = { path="../server-config" }
kdtree = { path="../utils/kdtree" }

[dev-dependencies]
Expand Down
11 changes: 6 additions & 5 deletions server/src/component/keystate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::component::SpecialActive;
use crate::protocol::{PlaneType, ServerKeyState};
use crate::config::PlanePrototypeRef;
use crate::protocol::ServerKeyState;

/// Known key state of a player.
///
Expand All @@ -22,17 +23,17 @@ pub struct KeyState {
}

impl KeyState {
pub fn strafe(&self, plane: &PlaneType) -> bool {
*plane == PlaneType::Mohawk && self.special
pub fn strafe(&self, plane: &PlanePrototypeRef) -> bool {
plane.special.is_strafe() && self.special
}

pub fn to_server(&self, plane: &PlaneType, active: &SpecialActive) -> ServerKeyState {
pub fn to_server(&self, plane: &PlanePrototypeRef, active: &SpecialActive) -> ServerKeyState {
ServerKeyState {
up: self.up,
down: self.down,
left: self.left,
right: self.right,
boost: *plane == PlaneType::Predator && active.0,
boost: plane.special.is_boost() && active.0,
strafe: self.strafe(plane),
stealth: self.stealthed,
flagspeed: self.flagspeed,
Expand Down
93 changes: 49 additions & 44 deletions server/src/consts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;

use nalgebra::vector;

use crate::config::MissilePrototypeRef;
use crate::protocol::*;
use crate::FireMissileInfo;

Expand Down Expand Up @@ -35,50 +36,54 @@ pub const GOLIATH_SPECIAL_INTERVAL: Duration = Duration::from_secs(1);

// TODO: Tornado
pub const TORNADO_SPECIAL_ENERGY: Energy = 0.9;
pub const TORNADO_MISSILE_DETAILS: [FireMissileInfo; 3] = [
FireMissileInfo {
pos_offset: vector![0.0, 40.1],
rot_offset: 0.0,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![15.0, 9.6],
rot_offset: -0.05,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![-15.0, 9.6],
rot_offset: 0.05,
ty: MobType::TornadoTripleMissile,
},
];
pub const TORNADO_INFERNO_MISSILE_DETAILS: [FireMissileInfo; 5] = [
FireMissileInfo {
pos_offset: vector![0.0, 40.1],
rot_offset: 0.0,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![30.0, 15.0],
rot_offset: -0.1,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![20.0, 25.0],
rot_offset: -0.05,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![-20.0, 25.0],
rot_offset: 0.05,
ty: MobType::TornadoTripleMissile,
},
FireMissileInfo {
pos_offset: vector![-30.0, 15.0],
rot_offset: 0.1,
ty: MobType::TornadoTripleMissile,
},
];
pub fn tornado_missile_details(proto: MissilePrototypeRef) -> [FireMissileInfo; 3] {
[
FireMissileInfo {
pos_offset: vector![0.0, 40.1],
rot_offset: 0.0,
proto,
},
FireMissileInfo {
pos_offset: vector![15.0, 9.6],
rot_offset: -0.05,
proto,
},
FireMissileInfo {
pos_offset: vector![-15.0, 9.6],
rot_offset: 0.05,
proto,
},
]
}
pub fn tornado_inferno_missile_details(proto: MissilePrototypeRef) -> [FireMissileInfo; 5] {
[
FireMissileInfo {
pos_offset: vector![0.0, 40.1],
rot_offset: 0.0,
proto,
},
FireMissileInfo {
pos_offset: vector![30.0, 15.0],
rot_offset: -0.1,
proto,
},
FireMissileInfo {
pos_offset: vector![20.0, 25.0],
rot_offset: -0.05,
proto,
},
FireMissileInfo {
pos_offset: vector![-20.0, 25.0],
rot_offset: 0.05,
proto,
},
FireMissileInfo {
pos_offset: vector![-30.0, 15.0],
rot_offset: 0.1,
proto,
},
]
}

pub const PROWLER_SPECIAL_ENERGY: Energy = 0.6;
pub const PROWLER_SPECIAL_DELAY: Duration = Duration::from_millis(1500);
15 changes: 6 additions & 9 deletions server/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ use hecs::EntityBuilder;
use uuid::Uuid;

use crate::component::*;
use crate::config::PlanePrototypeRef;
use crate::protocol::client::Login;
use crate::protocol::{FlagCode, PlaneType, Vector2};
use crate::resource::Config;
use crate::protocol::{FlagCode, Vector2};

/// Build a player
pub(crate) fn build_default_player(
login: &Login,
config: &Config,
proto: PlanePrototypeRef,
start_time: Instant,
this_frame: Instant,
) -> EntityBuilder {
let plane = PlaneType::Predator;
let info = &config.planes[plane];

let mut builder = EntityBuilder::new();
builder
.add(IsPlayer)
Expand All @@ -31,9 +28,9 @@ pub(crate) fn build_default_player(
.add(Rotation(0.0))
.add(Energy(1.0))
.add(Health(1.0))
.add(EnergyRegen(info.energy_regen))
.add(HealthRegen(info.health_regen))
.add(plane)
.add(EnergyRegen(proto.energy_regen))
.add(HealthRegen(proto.health_regen))
.add(proto)
.add(FlagCode::from_str(&login.flag.to_string()).unwrap_or(FlagCode::UnitedNations))
.add(Level(0))
.add(Score(0))
Expand Down
6 changes: 4 additions & 2 deletions server/src/event/player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::time::Duration;

use airmash_protocol::{PlaneType, PowerupType};
use hecs::Entity;
use smallvec::SmallVec;

use crate::config::PlanePrototypeRef;
use crate::protocol::PowerupType;

/// A new player has joined the game.
#[derive(Clone, Copy, Debug)]
pub struct PlayerJoin {
Expand Down Expand Up @@ -57,7 +59,7 @@ pub struct PlayerSpawn {
#[derive(Copy, Clone, Debug)]
pub struct PlayerChangePlane {
pub player: Entity,
pub old_plane: PlaneType,
pub old_proto: PlanePrototypeRef,
}

/// A player has obtained a powerup.
Expand Down
9 changes: 9 additions & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ pub mod protocol {
pub use airmash_protocol::*;
}

pub mod config {
pub use server_config::*;

pub type GameConfigRef = &'static GameConfig;
pub type PlanePrototypeRef = &'static PlanePrototype<'static, PtrRef>;
pub type MissilePrototypeRef = &'static MissilePrototype;
pub type SpecialPrototypeRef = &'static SpecialPrototype<'static, PtrRef>;
}

pub extern crate hecs;
pub extern crate nalgebra;

Expand Down
36 changes: 3 additions & 33 deletions server/src/resource/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::protocol::{
AccelScalar, Distance, Energy, EnergyRegen, Health, HealthRegen, MobType, MobType as Mob,
PlaneType as Plane, Rotation, RotationRate, Speed,
};
use crate::util::serde::*;
use crate::Vector2;

#[derive(Debug, Clone, Serialize, Deserialize, DeserializeOver)]
Expand Down Expand Up @@ -128,6 +129,7 @@ pub struct UpgradeInfos {
#[derive(Clone, Debug, Serialize, Deserialize, DeserializeOver)]
pub struct Config {
#[deserialize_over]
#[deprecated]
pub planes: PlaneInfos,
#[deserialize_over]
pub mobs: MobInfos,
Expand Down Expand Up @@ -259,6 +261,7 @@ impl Default for UpgradeInfos {
}
}

#[allow(deprecated)]
impl Default for Config {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -533,36 +536,3 @@ mod mob_defaults {
}
}
}

mod duration {
use std::time::Duration;

use serde::{Deserialize, Deserializer, Serializer};

pub(super) fn serialize<S: Serializer>(dur: &Duration, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_f64(dur.as_secs_f64())
}

pub(super) fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<Duration, D::Error> {
f64::deserialize(de).map(Duration::from_secs_f64)
}
}

mod option_duration {
use std::time::Duration;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub(super) fn serialize<S: Serializer>(
dur: &Option<Duration>,
ser: S,
) -> Result<S::Ok, S::Error> {
dur.map(|d| d.as_secs_f64()).serialize(ser)
}

pub(super) fn deserialize<'de, D: Deserializer<'de>>(
de: D,
) -> Result<Option<Duration>, D::Error> {
Ok(Option::deserialize(de)?.map(Duration::from_secs_f64))
}
}
15 changes: 14 additions & 1 deletion server/src/resource/game_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ops::Deref;

/// Flags to enable and/or disable engine features.
///
/// By default these configs are set as would be needed for an FFA gamemode.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug)]
pub struct GameConfig {
/// Whether or not to enable the default respawn logic.
///
Expand Down Expand Up @@ -38,6 +40,8 @@ pub struct GameConfig {
///
/// This is set to false by default.
pub always_upgraded: bool,

pub inner: server_config::GameConfig,
}

impl Default for GameConfig {
Expand All @@ -48,6 +52,15 @@ impl Default for GameConfig {
allow_damage: true,
spawn_upgrades: true,
always_upgraded: false,
inner: server_config::GameConfig::default(),
}
}
}

impl Deref for GameConfig {
type Target = server_config::GameConfig;

fn deref(&self) -> &Self::Target {
&self.inner
}
}
6 changes: 3 additions & 3 deletions server/src/system/collision.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::cmp::Ordering;
use std::time::Duration;

use airmash_protocol::PlaneType;
use itertools::Itertools;
use smallvec::SmallVec;

use crate::component::*;
use crate::config::PlanePrototypeRef;
use crate::consts::{self, hitcircles_for_plane};
use crate::event::{
EventBounce, MissileTerrainCollision, PlayerMissileCollision, PlayerMobCollision,
Expand Down Expand Up @@ -71,7 +71,7 @@ fn generate_player_collide_db(game: &mut AirmashGame) {

let query = game
.world
.query_mut::<(&Position, &Rotation, &PlaneType, &Team, &IsAlive)>()
.query_mut::<(&Position, &Rotation, &PlanePrototypeRef, &Team, &IsAlive)>()
.with::<IsPlayer>();
let mut entries = Vec::new();

Expand All @@ -80,7 +80,7 @@ fn generate_player_collide_db(game: &mut AirmashGame) {
continue;
}

for hc in hitcircles_for_plane(*plane) {
for hc in hitcircles_for_plane(plane.server_type) {
let offset = crate::util::rotate(hc.0, rot.0);

entries.push(Entry {
Expand Down
24 changes: 14 additions & 10 deletions server/src/system/handler/chat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::component::{IsPlayer, Position, SpecialActive, Team};
use crate::config::PlanePrototypeRef;
use crate::event::PacketEvent;
use crate::protocol::client::{Chat, Say, TeamChat, Whisper};
use crate::protocol::{server as s, PlaneType};
use crate::protocol::server as s;
use crate::AirmashGame;

#[handler]
Expand Down Expand Up @@ -62,21 +63,24 @@ fn on_whisper(event: &PacketEvent<Whisper>, game: &mut AirmashGame) {

#[handler]
fn on_say(event: &PacketEvent<Say>, game: &mut AirmashGame) {
let (&pos, &plane, &special, &team, _) =
match game
.world
.query_one_mut::<(&Position, &PlaneType, &SpecialActive, &Team, &IsPlayer)>(event.entity)
{
Ok(query) => query,
Err(_) => return,
};
let (&pos, &plane, &special, &team, _) = match game.world.query_one_mut::<(
&Position,
&PlanePrototypeRef,
&SpecialActive,
&Team,
&IsPlayer,
)>(event.entity)
{
Ok(query) => query,
Err(_) => return,
};

let packet = s::ChatSay {
id: event.entity.id() as _,
text: event.packet.text.clone(),
};

if plane == PlaneType::Prowler && special.0 {
if plane.special.is_stealth() && special.0 {
game.send_to_team_visible(team.0, pos.0, packet);
} else {
game.send_to_visible(pos.0, packet);
Expand Down

0 comments on commit 48bf0e3

Please sign in to comment.