use termcolor::{ColorSpec, WriteColor};
use crate::the_game::damage::Component;
use crate::the_game::quadrant::Quadrant;
use crate::{StResult, TheGame};
fn qstr<W: WriteColor>(
the_game: &TheGame,
sout: &mut W,
i: i32,
j: i32,
is_current: bool,
) -> StResult<()> {
let quadrant = Quadrant::new(i, j);
let value = the_game.quadrant_map[quadrant];
let emphasize = is_current;
value.draw(sout, emphasize)?;
Ok(())
}
pub fn l_range_scan<W: WriteColor>(the_game: &mut TheGame, sout: &mut W) -> StResult<()> {
let i = Component::LongRangeSensors; if the_game.damage.is_damaged(i.into(), false) {
the_game.damage.show_damage(sout, i)?;
return Ok(());
}
let q1: i32 = the_game.q1 as i32;
let q2: i32 = the_game.q2 as i32;
writeln!(sout, "{} FOR QUADRANT {} - {}", i.as_ref(), q1 + 1, q2 + 1)?;
for i in (q1 - 1)..=(q1 + 1) {
for j in (q2 - 1)..=(q2 + 1) {
write!(sout, " ")?;
sout.flush()?;
if (i < 0) || (i > 7) || (j < 0) || (j > 7) {
sout.set_color(ColorSpec::new().set_dimmed(true))?;
write!(sout, "***")?;
sout.flush()?;
sout.reset()?;
} else {
let quadrant = Quadrant::new(i as i32, j as i32);
the_game.quadrant_map[quadrant].show();
qstr(
the_game,
sout,
i as i32,
j as i32,
the_game.is_current_quadrant(i, j),
)?;
}
}
writeln!(sout)?;
}
Ok(())
}
pub fn galactic_records<W: WriteColor>(the_game: &TheGame, sout: &mut W) -> StResult<()> {
let i = Component::GalacticRecords; if the_game.damage.is_damaged(i.into(), false) {
the_game.damage.show_damage(sout, i)?;
return Ok(());
}
writeln!(
sout,
"CUMULATIVE GALACTIC MAP FOR STARDATE {}",
the_game.current_stardate
)?;
for i in 0..8 {
for j in 0..8 {
write!(sout, " ")?;
sout.flush()?;
qstr(
the_game,
sout,
i as i32,
j as i32,
the_game.is_current_quadrant(i, j),
)?;
}
writeln!(sout)?;
}
Ok(())
}
pub fn s_range_scan<W: WriteColor>(the_game: &mut TheGame, sout: &mut W, a: i32) -> StResult<()> {
the_game.check_condition(); if a == 0
{
the_game.check_for_hits(sout)?;
if the_game.energy <= 0.0 {
return Ok(());
}
}
let i = Component::ShortRangeSensors; if the_game.damage.is_damaged(i.into(), false) {
the_game.damage.show_damage(sout, i)?;
return Ok(());
}
for i in 0..8 {
for j in 0..8 {
write!(sout, "{} ", the_game.sector_map.sector_char_at_coords(i, j))?;
sout.flush()?;
}
write!(sout, " ")?;
sout.flush()?;
match i {
0 => {
writeln!(
sout,
"YEARS = {}",
the_game.game_defs.ending_stardate - the_game.current_stardate
)?;
}
1 => {
writeln!(sout, "STARDATE = {}", the_game.current_stardate)?;
}
2 => {
write!(sout, "CONDITION: ")?;
sout.set_color(&the_game.current_condition.get_color_spec())?;
writeln!(sout, "{}", the_game.current_condition.as_ref())?;
sout.reset()?;
}
3 => {
writeln!(sout, "QUADRANT = {} - {}", the_game.q1 + 1, the_game.q2 + 1)?;
}
4 => {
writeln!(sout, "SECTOR = {} - {}", the_game.s1 + 1, the_game.s2 + 1)?;
}
5 => {
writeln!(sout, "ENERGY = {:03}", the_game.energy)?; }
6 => {
writeln!(
sout,
"{} = {}",
Component::PhotonTorpedoes.as_ref(),
the_game.photo_torpedoes
)?;
}
7 => {
writeln!(sout, "KLINGONS LEFT = {}", the_game.total_klingons)?;
}
_ => {}
}
}
Ok(())
}