Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed May 15, 2024
1 parent 2d47a9c commit cef774c
Show file tree
Hide file tree
Showing 33 changed files with 454 additions and 94 deletions.
2 changes: 1 addition & 1 deletion battle_core/src/behavior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
pub mod feeling;
pub mod gesture;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Body {
StandUp,
Crouched,
Expand Down
10 changes: 9 additions & 1 deletion battle_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use strum::IntoEnumIterator;
pub const DEFAULT_SERVER_REP_ADDRESS: &str = "tcp://0.0.0.0:4255";
pub const DEFAULT_SERVER_PUB_ADDRESS: &str = "tcp://0.0.0.0:4256";
///
pub const TARGET_CYCLE_DURATION_US: u64 = 16666;
pub const TARGET_FPS: u64 = 60;
pub const SOLDIER_UPDATE_FREQ: u64 = 1;
pub const FLAGS_UPDATE_FREQ: u64 = 120;
Expand Down Expand Up @@ -41,7 +42,7 @@ pub const VISIBILITY_DEAD_MODIFIER: f32 = 0.0;
pub const VISIBILITY_UNCONSCIOUS_MODIFIER: f32 = 0.0;
///
pub const TILE_TYPE_OPACITY_SHORT_GRASS: f32 = 0.0;
pub const TILE_TYPE_OPACITY_MIDDLE_GRASS: f32 = 0.008;
pub const TILE_TYPE_OPACITY_MIDDLE_GRASS: f32 = 0.002;
pub const TILE_TYPE_OPACITY_HIGH_GRASS: f32 = 0.1;
pub const TILE_TYPE_OPACITY_DIRT: f32 = 0.0;
pub const TILE_TYPE_OPACITY_CONCRETE: f32 = 0.0;
Expand Down Expand Up @@ -87,6 +88,7 @@ pub const COVER_DISTANCE: i32 = 6;
// Visibility computing must consider firsts tiles differently
pub const VISIBILITY_FIRSTS: usize = 6;
pub const VISIBLE_STARTS_AT: f32 = 0.5;
pub const TARGET_ALTERATION_BY_OPACITY_FACTOR: f32 = 10.;
// When compute visibility, configure here each pixels step of line which me considered
pub const VISIBILITY_PIXEL_STEPS: usize = 5;
// When compute coverage, configure here each pixels step of line which me considered
Expand All @@ -103,6 +105,7 @@ pub const CAN_STANDUP_AFTER: u64 = TARGET_FPS * 60 * 10;
#[derive(Debug, Clone)]
pub struct ServerConfig {
pub send_debug_points: bool,
pub target_cycle_duration_us: u64,
pub flags_update_freq: u64,
pub soldier_update_freq: u64,
pub soldier_animate_freq: u64,
Expand All @@ -115,6 +118,7 @@ pub struct ServerConfig {
pub feeling_decreasing_freq: u64,
pub visibility_firsts: usize,
pub visible_starts_at: f32,
pub target_alteration_by_opacity_factor: f32,
pub visibility_idle_standup_modifier: f32,
pub visibility_idle_crouch_modifier: f32,
pub visibility_idle_lying_modifier: f32,
Expand Down Expand Up @@ -167,6 +171,7 @@ impl Default for ServerConfig {

Self {
send_debug_points: false,
target_cycle_duration_us: TARGET_CYCLE_DURATION_US,
/// Frequency of flags update

Check failure on line 175 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🛃 Clippy

unused doc comment

Check warning on line 175 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🔎 Check

unused doc comment

Check warning on line 175 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🧪 Test Suite

unused doc comment
flags_update_freq: FLAGS_UPDATE_FREQ,
/// Frequency of soldier update :

Check failure on line 177 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🛃 Clippy

unused doc comment

Check warning on line 177 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🔎 Check

unused doc comment

Check warning on line 177 in battle_core/src/config.rs

View workflow job for this annotation

GitHub Actions / 🧪 Test Suite

unused doc comment
Expand Down Expand Up @@ -194,6 +199,7 @@ impl Default for ServerConfig {
///
visibility_firsts: VISIBILITY_FIRSTS,
visible_starts_at: VISIBLE_STARTS_AT,
target_alteration_by_opacity_factor: TARGET_ALTERATION_BY_OPACITY_FACTOR,

visibility_idle_standup_modifier: VISIBILITY_IDLE_STANDUP_MODIFIER,
visibility_idle_crouch_modifier: VISIBILITY_IDLE_CROUCH_MODIFIER,
Expand Down Expand Up @@ -337,6 +343,7 @@ impl ServerConfig {
pub fn react(&mut self, message: &ChangeConfigMessage) {
match message {
ChangeConfigMessage::SendDebugPoints(v) => self.send_debug_points = *v,
ChangeConfigMessage::TargetCycleDuration(v) => self.target_cycle_duration_us = *v,
ChangeConfigMessage::SoldierUpdateFreq(v) => self.soldier_update_freq = *v,
ChangeConfigMessage::SoldierAnimateFreq(v) => self.soldier_animate_freq = *v,
ChangeConfigMessage::InteriorsUpdateFreq(v) => self.interiors_update_freq = *v,
Expand Down Expand Up @@ -410,6 +417,7 @@ impl Default for GuiConfig {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum ChangeConfigMessage {
SendDebugPoints(bool),
TargetCycleDuration(u64),
SoldierUpdateFreq(u64),
SoldierAnimateFreq(u64),
InteriorsUpdateFreq(u64),
Expand Down
17 changes: 17 additions & 0 deletions battle_core/src/entity/soldier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ impl Soldier {
let weapon_animation_type = WeaponAnimationType::from(&animation_type);
(animation_type, weapon_animation_type)
}

pub fn body(&self) -> Body {
match self.behavior {
Behavior::MoveTo(_) => Body::StandUp,
Behavior::MoveFastTo(_) => Body::StandUp,
Behavior::SneakTo(_) => Body::Lying,
Behavior::DriveTo(_) => Body::Crouched,
Behavior::RotateTo(_) => Body::Crouched,
Behavior::Idle(body) => body,
Behavior::Defend(_) => Body::Lying,
Behavior::Hide(_) => Body::Lying,
Behavior::Dead => Body::Lying,
Behavior::Unconscious => Body::Lying,
Behavior::SuppressFire(_) => Body::Lying,
Behavior::EngageSoldier(_) => Body::Lying,
}
}
}

impl From<&SoldierDeployment> for Soldier {
Expand Down
19 changes: 19 additions & 0 deletions battle_core/src/physics/visibility.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use bresenham::Bresenham;
use glam::Vec2;
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -61,6 +63,7 @@ pub struct Visibility {
pub from_soldier: Option<SoldierIndex>,
pub to: WorldPoint,
pub to_soldier: Option<SoldierIndex>,
pub altered_to: WorldPoint,
pub path_final_opacity: f32,
pub to_scene_item_opacity: f32,
pub opacity_segments: Vec<(WorldPoint, f32)>,
Expand All @@ -79,6 +82,7 @@ impl Visibility {
from: from_point,
from_soldier: Some(from_soldier.uuid()),
to: to_point,
altered_to: to_point,
to_soldier: Some(to_soldier.uuid()),
opacity_segments: vec![],
path_final_opacity: 999.,
Expand All @@ -96,6 +100,7 @@ impl Visibility {
to_soldier: &Soldier,
map: &Map,
) -> Self {
let mut rng = rand::thread_rng();
let from_point = from_soldier.world_point();
let to_point = to_soldier.world_point();
let last_shoot_frame_i = to_soldier.last_shoot_frame_i();
Expand All @@ -119,6 +124,16 @@ impl Visibility {
exclude_lasts,
);

// Compute a target point altered by opacity
let altered_to = if path_final_opacity > 0. {
let range = path_final_opacity * config.target_alteration_by_opacity_factor;
let x_change = rng.gen_range(-range..range);
let y_change = rng.gen_range(-range..range);
to_point.apply(Vec2::new(x_change, y_change))
} else {
to_point
};

to_soldier_item_opacity -= by_behavior_modifier;
let visible = to_soldier_item_opacity < config.visible_starts_at;

Expand All @@ -129,6 +144,7 @@ impl Visibility {
from_soldier: Some(from_soldier.uuid()),
to: to_point,
to_soldier: Some(to_soldier.uuid()),
altered_to,
opacity_segments,
path_final_opacity,
to_scene_item_opacity: to_soldier_item_opacity,
Expand All @@ -138,6 +154,7 @@ impl Visibility {
}
}

// FIXME BS NOW: add altered point (due to last tiles opacities)
pub fn between_soldier_and_point(
config: &ServerConfig,
from_soldier: &Soldier,
Expand All @@ -164,6 +181,7 @@ impl Visibility {
from_soldier: Some(from_soldier.uuid()),
to: *to_point,
to_soldier: None,
altered_to: *to_point,
opacity_segments,
path_final_opacity,
to_scene_item_opacity: to_soldier_item_opacity,
Expand All @@ -189,6 +207,7 @@ impl Visibility {
from_soldier: None,
to: *to_point,
to_soldier: None,
altered_to: *to_point,
opacity_segments,
path_final_opacity,
to_scene_item_opacity: to_soldier_item_opacity,
Expand Down
3 changes: 1 addition & 2 deletions battle_core/src/state/battle/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ impl BattleState {
soldier: &Soldier,
point: &WorldPoint,
exclude_lasts: usize,
) -> bool {
) -> Visibility {
Visibility::between_soldier_and_point(config, soldier, point, self.map(), exclude_lasts)
.visible
}
}
16 changes: 12 additions & 4 deletions battle_gui/src/engine/debug/gui/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use ggez::Context;

use battle_core::config::{
ChangeConfigMessage, FEELING_DECREASING_FREQ, INTERIORS_UPDATE_FREQ, SOLDIER_ANIMATE_FREQ,
SOLDIER_UPDATE_FREQ, TARGET_FPS, TILE_TYPE_OPACITY_BRICK_WALL, TILE_TYPE_OPACITY_CONCRETE,
TILE_TYPE_OPACITY_DEEP_WATER, TILE_TYPE_OPACITY_DIRT, TILE_TYPE_OPACITY_HEDGE,
TILE_TYPE_OPACITY_HIGH_GRASS, TILE_TYPE_OPACITY_LIGHT_UNDERBRUSH,
SOLDIER_UPDATE_FREQ, TARGET_CYCLE_DURATION_US, TARGET_FPS, TILE_TYPE_OPACITY_BRICK_WALL,
TILE_TYPE_OPACITY_CONCRETE, TILE_TYPE_OPACITY_DEEP_WATER, TILE_TYPE_OPACITY_DIRT,
TILE_TYPE_OPACITY_HEDGE, TILE_TYPE_OPACITY_HIGH_GRASS, TILE_TYPE_OPACITY_LIGHT_UNDERBRUSH,
TILE_TYPE_OPACITY_MIDDLE_GRASS, TILE_TYPE_OPACITY_MIDDLE_ROCK,
TILE_TYPE_OPACITY_MIDDLE_WOOD_LOGS, TILE_TYPE_OPACITY_MUD, TILE_TYPE_OPACITY_SHORT_GRASS,
TILE_TYPE_OPACITY_TRUNK, TILE_TYPE_OPACITY_UNDERBRUSH, TILE_TYPE_OPACITY_WATER,
Expand Down Expand Up @@ -38,6 +38,14 @@ impl Engine {
.striped(true)
.show(ui, |ui| {
for (name, value, min, max, default, message) in [
(
"TARGET_CYCLE_DURATION_US",
&mut self.server_config.target_cycle_duration_us,
0,
TARGET_CYCLE_DURATION_US * 5,
TARGET_CYCLE_DURATION_US,
ChangeConfigMessage::TargetCycleDuration,
),
(
"SOLDIER_UPDATE_FREQ",
&mut self.server_config.soldier_update_freq,
Expand Down Expand Up @@ -79,7 +87,7 @@ impl Engine {
ChangeConfigMessage::FeelingDecreasingFreq,
),
]
as [(_, _, _, _, _, fn(_) -> _); 5]
as [(_, _, _, _, _, fn(_) -> _); 6]
{
ui.label(name);
if ui.button("reset").clicked() {
Expand Down
6 changes: 6 additions & 0 deletions battle_gui/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub struct Engine {
hud: Hud,
a_control: MapControl,
b_control: MapControl,
//
first_copy_loaded: bool,
when_first_copy_messages: Vec<EngineMessage>,
}

impl Engine {
Expand All @@ -87,6 +90,7 @@ impl Engine {
a_control: MapControl,
b_control: MapControl,
apply: Vec<EngineMessage>,
when_first_copy_apply: Vec<EngineMessage>,
) -> GameResult<Engine> {
let mut gui_state = GuiState::new(*side, battle_state.map());
gui_state.set_saves(
Expand All @@ -112,6 +116,8 @@ impl Engine {
hud,
a_control,
b_control,
first_copy_loaded: false,
when_first_copy_messages: when_first_copy_apply,
};
engine.react(apply, ctx)?;

Expand Down
6 changes: 6 additions & 0 deletions battle_gui/src/engine/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl Engine {

self.sync_required.swap(false, Ordering::Relaxed);
self.battle_state = battle_state;

if !self.first_copy_loaded {
self.first_copy_loaded = true;
self.react(self.when_first_copy_messages.clone(), ctx)?;
self.when_first_copy_messages = vec![];
}
}
OutputMessage::BattleState(battle_state_message) => {
if self.gui_state.debug_physics_areas {
Expand Down
1 change: 1 addition & 0 deletions battle_gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ fn main() -> Result<(), GuiError> {
false,
vec![],
vec![],
vec![],
)
}
3 changes: 3 additions & 0 deletions battle_gui/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub fn run(
force_server_map: bool,
inputs: Vec<InputMessage>,
engine_apply: Vec<EngineMessage>,
engine_when_first_copy_apply: Vec<EngineMessage>,
) -> Result<(), GuiError> {
let sync_required = Arc::new(AtomicBool::new(true));
let stop_required: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
Expand All @@ -132,6 +133,7 @@ pub fn run(
let (output_sender, output_receiver) = unbounded();

if let Err(error) = EmbeddedServer::new(
server_config.clone(),
&resources.lib(),
input_receiver,
output_sender,
Expand Down Expand Up @@ -219,6 +221,7 @@ pub fn run(
a_control,
b_control,
engine_apply,
engine_when_first_copy_apply,
)?;

// FIXME BS NOW : Closing GUI don't close thread correctly and keep process running
Expand Down
5 changes: 4 additions & 1 deletion battle_gui/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Display for EmbeddedServerError {
}

pub struct EmbeddedServer {
config: ServerConfig,
resources: PathBuf,
map_name: Option<String>,
force_map: Option<Map>,
Expand All @@ -67,12 +68,14 @@ pub struct EmbeddedServer {

impl EmbeddedServer {
pub fn new(
config: ServerConfig,
resources: &Path,
gui_input_receiver: Receiver<Vec<InputMessage>>,
gui_output_sender: Sender<Vec<OutputMessage>>,
stop_required: Arc<AtomicBool>,
) -> Self {
Self {
config,
resources: resources.to_path_buf(),
map_name: None,
force_map: None,
Expand Down Expand Up @@ -112,7 +115,7 @@ impl EmbeddedServer {
.map_name
.as_ref()
.ok_or(EmbeddedServerError::MissingMapName)?;
let config = ServerConfig::default();
let config = self.config.clone();

let map = if let Some(map) = &self.force_map {
map.clone()
Expand Down
8 changes: 5 additions & 3 deletions battle_server/src/runner/gesture/engage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ impl Runner {
let target_soldier = self.battle_state.soldier(*engaged_soldier_index);

if target_soldier.can_be_designed_as_target() {
let point = target_soldier.world_point();
if let Some(weapon) = self.soldier_able_to_fire_on_point(soldier, &point) {
let (gesture_context, gesture) = self.engage_point_gesture(soldier, &point, weapon);
let target_soldier_point = target_soldier.world_point();
if let Some(engagement) =
self.soldier_able_to_fire_on_point(soldier, &target_soldier_point)
{
let (gesture_context, gesture) = self.engage_point_gesture(soldier, engagement);
return GestureResult::Handled(gesture_context, gesture);
}
}
Expand Down
Loading

0 comments on commit cef774c

Please sign in to comment.