Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Apr 1, 2024
1 parent c1b8814 commit c31d888
Show file tree
Hide file tree
Showing 41 changed files with 1,005 additions and 874 deletions.
29 changes: 27 additions & 2 deletions battle_core/src/state/battle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use crate::{
physics::{
event::{bullet::BulletFire, cannon_blast::CannonBlast, explosion::Explosion},
path::{Direction, PathMode},
utils::distance_between_points,
visibility::Visibilities,
},
sync::BattleStateCopy,
types::{
SoldierBoard, SoldierIndex, SoldiersOnBoard, SquadComposition, SquadUuid, VehicleBoard,
VehicleIndex,
Distance, SoldierBoard, SoldierIndex, SoldiersOnBoard, SquadComposition, SquadUuid,
VehicleBoard, VehicleIndex, WorldPoint,
},
utils::{vehicle_board_from_soldiers_on_board, WorldShape},
};
Expand Down Expand Up @@ -404,6 +405,30 @@ impl BattleState {
pub fn b_morale(&self) -> &Morale {
&self.b_morale
}

// FIXME BS NOW : use this too for soldiers gestures : for now, we check from leader,
// but soldier must defend himself too if is away
pub fn visible_soldier_in_circle(
&self,
point: &WorldPoint,
radius: &Distance,
side: &Side,
) -> bool {
self.visibilities()
.visibles_soldiers()
.iter()
.any(|visibility| {
if let Some(soldier_index) = visibility.to_soldier {
let soldier = self.soldier(soldier_index);
if soldier.side() == side
&& &distance_between_points(point, &soldier.world_point()) <= radius
{
return true;
}
}
false
})
}
}

#[derive(Debug)]
Expand Down
12 changes: 10 additions & 2 deletions battle_gui/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use std::thread;

use battle_core::config::{ServerConfig, DEFAULT_SERVER_PUB_ADDRESS, DEFAULT_SERVER_REP_ADDRESS};
use battle_core::message::{InputMessage, OutputMessage};
use battle_core::network::error::NetworkError;
use battle_core::network::server::Server;
use battle_core::state::battle::builder::{BattleStateBuilder, BattleStateBuilderError};
use battle_core::state::battle::message::BattleStateMessage;
use battle_server::runner::worker::Workers;
use battle_server::runner::Runner;
use crossbeam_channel::{unbounded, Receiver, Sender};

Expand Down Expand Up @@ -94,7 +96,10 @@ impl EmbeddedServer {
.as_ref()
.ok_or(EmbeddedServerError::MissingMapName)?;
let config = ServerConfig::default();
let state = BattleStateBuilder::new(map_name, self.resources.clone()).build()?;
let mut state = Arc::new(RwLock::new(
BattleStateBuilder::new(map_name, self.resources.clone()).build()?,
));
let workers = Workers::new(config.clone(), state.clone());

let stop_required_ = self.stop_required.clone();
thread::Builder::new()
Expand All @@ -103,6 +108,7 @@ impl EmbeddedServer {
println!("Start runner");
match Runner::new(
config,
workers,
runner_input_receiver,
runner_output_sender,
stop_required_,
Expand All @@ -120,6 +126,8 @@ impl EmbeddedServer {
})
.unwrap();

state.react(&BattleStateMessage::IncrementFrameI, 1);

Ok((runner_input_sender, runner_output_receiver))
}

Expand Down
8 changes: 6 additions & 2 deletions battle_server/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use battle_server::runner::worker::Workers;
use crossbeam_channel::unbounded;
use env_logger::Env;
use std::path::PathBuf;
Expand Down Expand Up @@ -61,13 +62,16 @@ fn main() -> Result<(), Error> {

let stop_required_ = stop_required.clone();
let config = ServerConfig::default();
let battle_state = BattleStateBuilder::new(map_name, resources.clone()).build()?;
let mut state = Arc::new(BattleStateBuilder::new(map_name, resources.clone()).build()?);
let workers = Workers::new(config.clone(), state.clone());

let mut runner = Runner::new(
config,
workers,
server_input_receiver,
server_output_sender,
stop_required_,
battle_state,
state,
);

runner.run()?;
Expand Down
34 changes: 0 additions & 34 deletions battle_server/src/runner/behavior/blast.rs

This file was deleted.

26 changes: 0 additions & 26 deletions battle_server/src/runner/behavior/bullet.rs

This file was deleted.

22 changes: 0 additions & 22 deletions battle_server/src/runner/behavior/death.rs

This file was deleted.

107 changes: 0 additions & 107 deletions battle_server/src/runner/fight/choose.rs
Original file line number Diff line number Diff line change
@@ -1,108 +1 @@
use rand::seq::SliceRandom;

use battle_core::{
entity::soldier::Soldier,
physics::{utils::distance_between_points, visibility::Visibility},
state::battle::BattleState,
types::{Distance, SoldierIndex, SquadUuid},
};

use crate::runner::Runner;

pub const NEAR_SOLDIERS_DISTANCE_METERS: i64 = 7;

pub enum ChooseMethod {
RandomFromNearest,
}
impl ChooseMethod {
fn choose(
&self,
battle_state: &BattleState,
visibles: Vec<&Visibility>,
) -> Option<SoldierIndex> {
match self {
Self::RandomFromNearest => self.choose_random_from_nearest(battle_state, visibles),
}
}

fn choose_random_from_nearest(
&self,
battle_state: &BattleState,
visibles: Vec<&Visibility>,
) -> Option<SoldierIndex> {
if let Some(visibility) = visibles.first() {
let soldier = battle_state.soldier(
visibility
.to_soldier
.expect("visibles_soldiers_by must returned with to_soldier"),
);
let soldier_position = soldier.world_point();
let near_soldiers: Vec<&Soldier> = visibles
.iter()
.map(|v| {
battle_state.soldier(
v.to_soldier
.expect("visibles_soldiers_by must returned with to_soldier"),
)
})
.filter(|s| {
distance_between_points(&soldier_position, &s.world_point())
< Distance::from_meters(NEAR_SOLDIERS_DISTANCE_METERS)
})
.collect();

return near_soldiers
.choose(&mut rand::thread_rng())
.map(|s| s.uuid());
}

None
}
}

impl Runner {
// TODO : choose soldier according to distance, weapon type, etc
// TODO : choose soldier according to other squad targets (distribution)
// TODO : don't make it if soldier is driver, working assistant, etc
pub fn soldier_find_opponent_to_target(
&self,
soldier: &Soldier,
squad_index: Option<&SquadUuid>,
method: &ChooseMethod,
) -> Option<&Soldier> {
let mut visibles = self
.battle_state
.visibilities()
.visibles_soldiers_by_soldier(soldier);

visibles.retain(|v| {
self.battle_state
.soldier(v.to_soldier.expect("filtered previously"))
.can_be_designed_as_target()
});

if let Some(squad_index) = squad_index {
visibles.retain(|v| {
self.battle_state
.soldier(v.to_soldier.expect("filtered previously"))
.squad_uuid()
== *squad_index
})
}

visibles.sort_by(|a, b| {
a.distance
.millimeters()
.partial_cmp(&b.distance.millimeters())
.expect("Must be i64")
});

if soldier.behavior().is_hide() {
visibles.retain(|v| v.distance <= self.config.hide_maximum_rayon)
}

method
.choose(&self.battle_state, visibles)
.map(|i| self.battle_state.soldier(i))
}
}
1 change: 0 additions & 1 deletion battle_server/src/runner/fight/mod.rs

This file was deleted.

10 changes: 5 additions & 5 deletions battle_server/src/runner/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use super::{message::RunnerMessage, Runner};

impl Runner {
pub fn tick_flags(&self) -> Vec<RunnerMessage> {
if self.battle_state.frame_i() % self.config.soldier_update_freq() == 0 {
if self.battle_state().frame_i() % self.config.soldier_update_freq() == 0 {
let mut new_ownerships = vec![];
for (flag_name, ownership) in self.battle_state.flags().ownerships() {
let flag = self.battle_state.map().flag(flag_name);
for (flag_name, ownership) in self.battle_state().flags().ownerships() {
let flag = self.battle_state().map().flag(flag_name);

Check failure on line 16 in battle_server/src/runner/flag.rs

View workflow job for this annotation

GitHub Actions / 🔎 Check

temporary value dropped while borrowed
let a_inside = self
.battle_state
.battle_state()
.there_is_side_soldier_in(&Side::A, flag.shape());
let b_inside = self
.battle_state
.battle_state()
.there_is_side_soldier_in(&Side::B, flag.shape());

let new_ownership = match (ownership, a_inside, b_inside) {
Expand Down
Loading

0 comments on commit c31d888

Please sign in to comment.