Skip to content

Commit

Permalink
net: removed redundant information
Browse files Browse the repository at this point in the history
also some QoL fixes
  • Loading branch information
lewis-weinberger committed Feb 12, 2023
1 parent ac81db1 commit 38bcf74
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 58 deletions.
2 changes: 2 additions & 0 deletions scripts/client
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
cargo run --bin client 127.0.0.1:5000
2 changes: 2 additions & 0 deletions scripts/server
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
cargo run --bin server 127.0.0.1:5000 4 5 48 1 true
4 changes: 2 additions & 2 deletions src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub const POSITIONS: [Option<Point>; 4] = [Some((40, 1)), Some((1, 5)), Some((45
pub const TARGETS: [Option<Point>; 4] = [Some((1, 46)), Some((45, 45)), Some((1, 1)), None];

pub const GUARDS: [Option<(Point, Direction)>; 5] = [
Some(((5, 5), Direction::Down)),
Some(((15, 12), Direction::Up)),
Some(((10, 45), Direction::Right)),
Some(((20, 30), Direction::Left)),
Some(((31, 20), Direction::Up)),
Some(((31, 30), Direction::Up)),
Some(((41, 10), Direction::Left)),
];
17 changes: 5 additions & 12 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::fmt;
use std::time::Duration;

// View-cone parameters
const LENGTH: i16 = 8;
const WIDTH: usize = 6;
const LENGTH: i16 = 10;
const WIDTH: usize = 8;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum Status {
Expand Down Expand Up @@ -254,7 +254,6 @@ pub struct Game {
pub timer: Duration,
pub defender: usize,
pub player: usize,
pub pos: Option<Point>,
pub positions: Vec<Option<Point>>,
pub guards: Vec<Option<(Point, Direction)>>,
pub targets: Vec<Option<Point>>,
Expand All @@ -265,7 +264,6 @@ impl Game {
pub fn new(cli: ServerCli) -> Self {
let address = cli.address;
let timer = Duration::from_secs((cli.timer * 60).into());
let pos = None;
let player = 0;

let players: usize;
Expand Down Expand Up @@ -302,7 +300,6 @@ impl Game {
timer,
defender,
player,
pos,
positions,
guards,
targets,
Expand Down Expand Up @@ -342,7 +339,6 @@ impl Game {
pub fn turn(&self, player: usize, current: usize) -> MsgToClient {
let turn = player == current;
let defender = player == self.defender;
let pos = self.positions[current];
let quit = if (defender && self.quit == Status::AttackerVictory)
|| (!defender && self.quit == Status::DefenderVictory)
{
Expand All @@ -354,7 +350,6 @@ impl Game {
MsgToClient {
turn,
defender,
pos,
positions: self.positions.clone(),
guards: self.guards.clone(),
quit,
Expand All @@ -370,7 +365,6 @@ impl Game {

/// Client-side turn processing
pub fn display<T: UserInterface>(&mut self, ui: &mut T, msg: &MsgToClient) -> Result<()> {
self.pos = msg.pos;
self.positions = msg.positions.clone();
self.guards = msg.guards.clone();
self.quit = msg.quit;
Expand All @@ -382,7 +376,7 @@ impl Game {
pub fn play<T: UserInterface>(&mut self, defender: bool, ui: &mut T) -> Result<MsgToServer> {
ui.input(self, defender)?;
Ok(MsgToServer {
new: self.pos,
new: self.positions[self.player],
guards: self.guards.clone(),
quit: self.quit,
})
Expand Down Expand Up @@ -441,13 +435,12 @@ impl Game {

/// Move player position
pub fn move_player(&mut self, dx: i16, dy: i16) {
if let Some((x, y)) = self.pos {
if let Some((x, y)) = self.positions[self.player] {
let x2 = (x as i16) + dx;
let y2 = (y as i16) + dy;
if let Some(tile) = self.map.at(x2 as usize, y2 as usize) {
if tile == Tile::Floor {
self.pos = Some((x2 as u8, y2 as u8));
self.positions[self.player] = self.pos;
self.positions[self.player] = Some((x2 as u8, y2 as u8));
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub struct MsgToClient {
pub turn: bool,
// Is the player the defender?
pub defender: bool,
// Player's position (if not defender and if alive)
pub pos: Option<Point>,
// Players' positions (if alive)
// TODO: cleanup redundant info
pub positions: Vec<Option<Point>>,
// Guards' positions (if alive)
pub guards: Vec<Option<(Point, Direction)>>,
Expand Down Expand Up @@ -115,7 +112,6 @@ impl Server {
for i in 0..game.players {
let mut g = game.clone();
g.player = i;
g.pos = g.positions[i];
clients.push(ClientHandle::new(&listener, g)?);
}

Expand Down
89 changes: 49 additions & 40 deletions src/ui/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use crossterm::{
cursor::{Hide, MoveTo, Show},
event::{poll, read, Event, KeyCode},
execute, queue,
style::{Print, PrintStyledContent, Stylize},
style::{PrintStyledContent, Stylize},
terminal::{self, Clear, ClearType},
};
use std::io::{self, Stdout, Write};
use std::ops::Drop;
use std::time::{Duration, Instant};

const TIMEOUT: u64 = 300;
const ACTIONS: isize = 5;
const ATTACKER_ACTIONS: isize = 5;
const DEFENDER_ACTIONS: isize = 8;

pub struct Terminal {
stdout: Stdout,
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Terminal {
KeyCode::Char(c) => match c {
'q' => {
game.quit = Status::Quit;
return ACTIONS;
return DEFENDER_ACTIONS;
}
'[' => game.rotate_guard(self.guard, false),
']' => game.rotate_guard(self.guard, true),
Expand All @@ -73,7 +74,7 @@ impl Terminal {
KeyCode::Char(c) => match c {
'q' => {
game.quit = Status::Quit;
return ACTIONS;
return ATTACKER_ACTIONS;
}
'.' => (),
_ => return 0,
Expand All @@ -86,7 +87,7 @@ impl Terminal {
/// Display game status
fn status(&mut self, game: &Game, ap: isize, rem: Duration) -> Result<()> {
self.message(&format!(
"Attackers: {}, Guards: {}, Actions: {}, Turn Time: {}s",
"Your turn! Attackers: {}, Guards: {}, Actions: {}, Turn Time: {}s",
game.positions.iter().filter(|&x| x.is_some()).count(),
game.guards.iter().filter(|&x| x.is_some()).count(),
ap,
Expand Down Expand Up @@ -138,7 +139,7 @@ impl UserInterface for Terminal {
None
};
} else {
self.centre = game.pos;
self.centre = game.positions[game.player];
}

// Display map
Expand All @@ -152,39 +153,43 @@ impl UserInterface for Terminal {
}
}

for (i, (pos, _dir)) in game.guards.iter().flatten().enumerate() {
// View cones
for (pos, tile) in game.view_cone(i).iter() {
if let Some((x, y)) = self.map_to_display(*pos) {
queue!(
self.stdout,
MoveTo(x, y),
PrintStyledContent(tile.to_string().on_red())
)?;
for (i, guard) in game.guards.iter().enumerate() {
if let Some((pos, _dir)) = guard {
// View cones
for (pos, tile) in game.view_cone(i).iter() {
if let Some((x, y)) = self.map_to_display(*pos) {
queue!(
self.stdout,
MoveTo(x, y),
PrintStyledContent(tile.to_string().on_red())
)?;
}
}
}

// Display guards
if let Some((x, y)) = self.map_to_display(*pos) {
let g = if defender && i == self.guard {
"G".yellow()
} else {
"G".cyan()
};
queue!(self.stdout, MoveTo(x, y), PrintStyledContent(g))?;
// Display guards
if let Some((x, y)) = self.map_to_display(*pos) {
let g = if defender && i == self.guard {
"G".yellow()
} else {
"G".cyan()
};
queue!(self.stdout, MoveTo(x, y), PrintStyledContent(g))?;
}
}
}

// Display players
for (i, pos) in game.positions.iter().flatten().enumerate() {
if !defender || game.visible(i) {
if let Some((x, y)) = self.map_to_display(*pos) {
let p = if i != game.player {
"A".white()
} else {
"A".magenta()
};
queue!(self.stdout, MoveTo(x, y), PrintStyledContent(p))?;
for (i, player) in game.positions.iter().enumerate() {
if let Some(pos) = player {
if !defender || game.visible(i) {
if let Some((x, y)) = self.map_to_display(*pos) {
let p = if i != game.player {
"A".white()
} else {
"A".green().bold()
};
queue!(self.stdout, MoveTo(x, y), PrintStyledContent(p))?;
}
}
}
}
Expand All @@ -207,7 +212,7 @@ impl UserInterface for Terminal {
queue!(
self.stdout,
MoveTo(0, self.size.1 - 1),
Print(msg),
PrintStyledContent(msg.magenta()),
Clear(ClearType::UntilNewLine),
)?;
self.stdout.flush()?;
Expand All @@ -219,15 +224,19 @@ impl UserInterface for Terminal {
let timer = Instant::now();

let mut detected: isize = 3;
let mut actions: isize = ACTIONS;
let mut actions: isize = if defender {
DEFENDER_ACTIONS
} else {
ATTACKER_ACTIONS
};
while actions > 0 {
if let Some(remaining) = game.timer.checked_sub(timer.elapsed()) {
self.status(game, actions, remaining)?;
} else {
break;
}

if !defender && game.pos.is_none() {
if !defender && game.positions[game.player].is_none() {
break;
}

Expand All @@ -251,7 +260,7 @@ impl UserInterface for Terminal {

// Check for guard elimination
for guard in game.guards.iter_mut() {
if let Some(pos) = game.pos {
if let Some(pos) = game.positions[game.player] {
if let Some((guard_pos, _)) = guard {
if *guard_pos == pos {
*guard = None;
Expand All @@ -265,12 +274,12 @@ impl UserInterface for Terminal {
detected -= 1;
}
if detected == 0 {
game.pos = None;
game.positions[game.player] = None;
}

self.display(game, defender)?;
if !defender && game.pos == game.targets[game.player] {
game.pos = None;
if !defender && game.positions[game.player] == game.targets[game.player] {
game.positions[game.player] = None;
break;
}
}
Expand Down

0 comments on commit 38bcf74

Please sign in to comment.