| 1 | |
| 2 | |
| 3 | |
| 4 | use std::collections::HashMap; |
| 5 | |
| 6 | use bevy::input::ButtonInput; |
| 7 | use bevy::prelude::*; |
| 8 | use bevy::time::{Real, Time}; |
| 9 | |
| 10 | use crate::audio::SoundEvent; |
| 11 | use crate::economy::advance_clock; |
| 12 | use crate::model::{ |
| 13 | FrontState, Good, HourTick, MIN_USABLE_CONDITION, Network, Notify, Outcome, Selected, SimClock, |
| 14 | Tool, Train, Treasury, |
| 15 | }; |
| 16 | use crate::seasons::SeasonState; |
| 17 | use crate::trains::TrainCounter; |
| 18 | |
| 19 | |
| 20 | pub const AMMO_NEED: f32 = 1.0; |
| 21 | pub const FOOD_NEED: f32 = 0.8; |
| 22 | pub const TROOPS_NEED: f32 = 0.15; |
| 23 | |
| 24 | |
| 25 | pub fn hourly_need(g: Good, food_mult: f32) -> f32 { |
| 26 | match g { |
| 27 | Good::Ammo => AMMO_NEED, |
| 28 | Good::Food => FOOD_NEED * food_mult, |
| 29 | Good::Troops => TROOPS_NEED, |
| 30 | Good::Wounded => 0.0, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | const SUPPLY_RATE: f32 = 0.1; |
| 35 | |
| 36 | const LINE_RATE: f32 = 0.004; |
| 37 | pub const NEUTRAL_SUPPLY: f32 = 0.5; |
| 38 | |
| 39 | const WOUNDED_BASE: f32 = 0.2; |
| 40 | const WOUNDED_SHORTAGE: f32 = 0.5; |
| 41 | const WOUNDED_CAP: f32 = 200.0; |
| 42 | const LOW_SUPPLY: f32 = 0.35; |
| 43 | const NOTICE_COOLDOWN_HOURS: u32 = 24; |
| 44 | const ADVANCE_LINE: f32 = 0.5; |
| 45 | const RETREAT_LINE: f32 = -0.5; |
| 46 | |
| 47 | |
| 48 | const RAID_EVERY_HOURS: u32 = 24; |
| 49 | const RAID_CHANCE: f64 = 0.35; |
| 50 | |
| 51 | const RAID_DAMAGE_MIN: f32 = 0.25; |
| 52 | const RAID_DAMAGE_MAX: f32 = 0.5; |
| 53 | |
| 54 | const SELF_REPAIR_PER_HOUR: f32 = 0.01; |
| 55 | |
| 56 | const FRONT_ZONE_X: f32 = 100.0; |
| 57 | |
| 58 | const RAID_FX_SECS: f32 = 2.5; |
| 59 | |
| 60 | const RAID_PLANE_REACH: Vec2 = Vec2::new(480.0, 140.0); |
| 61 | |
| 62 | const RAID_BURSTS: [(f32, f32); 4] = [(0.6, 0.18), (0.85, 0.42), (1.1, 0.62), (1.35, 0.84)]; |
| 63 | const RAID_BURST_SECS: f32 = 1.0; |
| 64 | |
| 65 | const RAID_FLASH_START: f32 = 0.5; |
| 66 | |
| 67 | |
| 68 | const LINE_OFFSET_X: f32 = 70.0; |
| 69 | const LINE_SWING_X: f32 = 120.0; |
| 70 | const LINE_EXTENT_Y: f32 = 360.0; |
| 71 | const JAG_STEP: f32 = 12.0; |
| 72 | const JAG_AMPLITUDE: f32 = 5.0; |
| 73 | const TOOTH_SPACING: f32 = 15.0; |
| 74 | const TOOTH_LEN: f32 = 8.0; |
| 75 | const HATCH_STEP: f32 = 14.0; |
| 76 | const HATCH_LEN: f32 = 36.0; |
| 77 | const BAR_W: f32 = 90.0; |
| 78 | const BAR_H: f32 = 8.0; |
| 79 | |
| 80 | const BAR_DY: f32 = 16.0; |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | fn step_sector(stock: [f32; 4], front: FrontState, food_mult: f32) -> ([f32; 4], FrontState) { |
| 87 | let mut stock = stock; |
| 88 | let mut ratio_sum = 0.0; |
| 89 | for g in [Good::Ammo, Good::Food, Good::Troops] { |
| 90 | let idx = g.idx(); |
| 91 | let need = hourly_need(g, food_mult); |
| 92 | let taken = need.min(stock[idx].max(0.0)); |
| 93 | stock[idx] -= taken; |
| 94 | ratio_sum += taken / need; |
| 95 | } |
| 96 | let ratio = ratio_sum / 3.0; |
| 97 | let supply = front.supply + (ratio - front.supply) * SUPPLY_RATE; |
| 98 | let line = (front.line + (supply - NEUTRAL_SUPPLY) * LINE_RATE).clamp(-1.0, 1.0); |
| 99 | let wounded = stock[Good::Wounded.idx()] + WOUNDED_BASE + (1.0 - supply) * WOUNDED_SHORTAGE; |
| 100 | stock[Good::Wounded.idx()] = wounded.min(WOUNDED_CAP); |
| 101 | (stock, FrontState { line, supply }) |
| 102 | } |
| 103 | |
| 104 | |
| 105 | pub fn repair_step(condition: f32) -> f32 { |
| 106 | (condition + SELF_REPAIR_PER_HOUR).min(1.0) |
| 107 | } |
| 108 | |
| 109 | |
| 110 | fn judge(net: &Network) -> Option<Outcome> { |
| 111 | let lines: Vec<f32> = net |
| 112 | .nodes |
| 113 | .iter() |
| 114 | .filter_map(|n| n.front.map(|f| f.line)) |
| 115 | .collect(); |
| 116 | if lines.iter().any(|&l| l <= -1.0) { |
| 117 | Some(Outcome::Defeat) |
| 118 | } else if !lines.is_empty() && lines.iter().all(|&l| l >= 1.0) { |
| 119 | Some(Outcome::Victory) |
| 120 | } else { |
| 121 | None |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | fn notice_due(last: Option<u32>, now: u32) -> bool { |
| 127 | last.is_none_or(|h| now.saturating_sub(h) >= NOTICE_COOLDOWN_HOURS) |
| 128 | } |
| 129 | |
| 130 | |
| 131 | fn step_fronts(net: &mut Network, runtime: &mut FrontRuntime, now: u32, food_mult: f32) -> Vec<String> { |
| 132 | let mut news = Vec::new(); |
| 133 | for (i, node) in net.nodes.iter_mut().enumerate() { |
| 134 | let Some(old) = node.front else { continue }; |
| 135 | let (stock, new) = step_sector(node.stock, old, food_mult); |
| 136 | node.stock = stock; |
| 137 | node.front = Some(new); |
| 138 | if new.supply < LOW_SUPPLY && notice_due(runtime.low_notice.get(&i).copied(), now) { |
| 139 | runtime.low_notice.insert(i, now); |
| 140 | news.push(format!("{} is running out of supplies!", node.name)); |
| 141 | } |
| 142 | if old.line < ADVANCE_LINE && new.line >= ADVANCE_LINE { |
| 143 | news.push(format!("{} advances", node.name)); |
| 144 | } |
| 145 | if old.line > RETREAT_LINE && new.line <= RETREAT_LINE { |
| 146 | news.push(format!("{} is falling back", node.name)); |
| 147 | } |
| 148 | } |
| 149 | news |
| 150 | } |
| 151 | |
| 152 | |
| 153 | fn raid_candidates(net: &Network) -> Vec<usize> { |
| 154 | net.tracks |
| 155 | .iter() |
| 156 | .enumerate() |
| 157 | .filter(|(_, t)| { |
| 158 | t.condition >= MIN_USABLE_CONDITION |
| 159 | && (net.nodes[t.a.0].pos.x > FRONT_ZONE_X || net.nodes[t.b.0].pos.x > FRONT_ZONE_X) |
| 160 | }) |
| 161 | .map(|(i, _)| i) |
| 162 | .collect() |
| 163 | } |
| 164 | |
| 165 | |
| 166 | pub(crate) fn air_raid(net: &mut Network, runtime: &mut FrontRuntime) -> Option<(usize, String)> { |
| 167 | let candidates = raid_candidates(net); |
| 168 | if candidates.is_empty() { |
| 169 | return None; |
| 170 | } |
| 171 | let i = candidates[rand::random_range(0..candidates.len())]; |
| 172 | let track = net.tracks[i]; |
| 173 | let a = &net.nodes[track.a.0]; |
| 174 | let b = &net.nodes[track.b.0]; |
| 175 | let text = format!("Air raid! Line {}-{} damaged", a.name, b.name); |
| 176 | let (pa, pb) = (a.pos, b.pos); |
| 177 | let loss: f32 = rand::random_range(RAID_DAMAGE_MIN..RAID_DAMAGE_MAX); |
| 178 | net.tracks[i].condition = (track.condition - loss).max(0.0); |
| 179 | runtime.raids.push(RaidFx { |
| 180 | a: pa, |
| 181 | b: pb, |
| 182 | age: 0.0, |
| 183 | }); |
| 184 | Some((i, text)) |
| 185 | } |
| 186 | |
| 187 | |
| 188 | pub(crate) struct RaidFx { |
| 189 | |
| 190 | a: Vec2, |
| 191 | b: Vec2, |
| 192 | |
| 193 | age: f32, |
| 194 | } |
| 195 | |
| 196 | |
| 197 | #[derive(Resource, Default)] |
| 198 | pub(crate) struct FrontRuntime { |
| 199 | |
| 200 | hours: u32, |
| 201 | |
| 202 | low_notice: HashMap<usize, u32>, |
| 203 | pub(crate) raids: Vec<RaidFx>, |
| 204 | } |
| 205 | |
| 206 | #[derive(Component)] |
| 207 | struct OutcomeOverlay; |
| 208 | |
| 209 | pub struct FrontPlugin; |
| 210 | |
| 211 | impl Plugin for FrontPlugin { |
| 212 | fn build(&self, app: &mut App) { |
| 213 | app.init_resource::<FrontRuntime>().add_systems( |
| 214 | Update, |
| 215 | ( |
| 216 | |
| 217 | front_hour_system.after(advance_clock), |
| 218 | restart_system, |
| 219 | outcome_overlay, |
| 220 | draw_front, |
| 221 | ) |
| 222 | .chain(), |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | #[allow(clippy::too_many_arguments, reason = "bevy system parameters")] |
| 228 | pub(crate) fn front_hour_system( |
| 229 | mut ticks: MessageReader<HourTick>, |
| 230 | mut net: ResMut<Network>, |
| 231 | mut outcome: ResMut<Outcome>, |
| 232 | mut clock: ResMut<SimClock>, |
| 233 | season: Res<SeasonState>, |
| 234 | mut runtime: ResMut<FrontRuntime>, |
| 235 | mut notify: MessageWriter<Notify>, |
| 236 | mut sounds: MessageWriter<SoundEvent>, |
| 237 | ) { |
| 238 | for _ in ticks.read() { |
| 239 | runtime.hours += 1; |
| 240 | if *outcome != Outcome::Ongoing { |
| 241 | continue; |
| 242 | } |
| 243 | for news in step_fronts(&mut net, &mut runtime, clock.hour, season.season.food_mult()) { |
| 244 | notify.write(Notify(news)); |
| 245 | } |
| 246 | |
| 247 | let cut: Vec<bool> = net.tracks.iter().map(|t| net.is_cut(t)).collect(); |
| 248 | for (track, cut) in net.tracks.iter_mut().zip(cut) { |
| 249 | if !cut { |
| 250 | track.condition = repair_step(track.condition); |
| 251 | } |
| 252 | } |
| 253 | if let Some(result) = judge(&net) { |
| 254 | *outcome = result; |
| 255 | clock.speed = 0; |
| 256 | let text = match result { |
| 257 | Outcome::Victory => "OPERATION SUCCESSFUL", |
| 258 | _ => "THE FRONT HAS COLLAPSED", |
| 259 | }; |
| 260 | sounds.write(match result { |
| 261 | Outcome::Victory => SoundEvent::Victory, |
| 262 | _ => SoundEvent::Defeat, |
| 263 | }); |
| 264 | notify.write(Notify(text.to_string())); |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | if runtime.hours.is_multiple_of(RAID_EVERY_HOURS) |
| 269 | && !season.fog |
| 270 | && rand::random_bool(RAID_CHANCE * f64::from(season.season.raid_mult())) |
| 271 | && let Some((_, text)) = air_raid(&mut net, &mut runtime) |
| 272 | { |
| 273 | sounds.write(SoundEvent::Raid); |
| 274 | notify.write(Notify(text)); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
| 280 | fn restart_system(world: &mut World) { |
| 281 | let pressed = { |
| 282 | let keys = world.resource::<ButtonInput<KeyCode>>(); |
| 283 | keys.just_pressed(KeyCode::Enter) || keys.just_pressed(KeyCode::NumpadEnter) |
| 284 | }; |
| 285 | if !pressed || *world.resource::<Outcome>() == Outcome::Ongoing { |
| 286 | return; |
| 287 | } |
| 288 | reset_game(world); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | |
| 293 | pub fn reset_game(world: &mut World) { |
| 294 | *world.resource_mut::<Network>() = Network::default(); |
| 295 | *world.resource_mut::<Treasury>() = Treasury::default(); |
| 296 | *world.resource_mut::<SimClock>() = SimClock::default(); |
| 297 | *world.resource_mut::<Outcome>() = Outcome::Ongoing; |
| 298 | *world.resource_mut::<Tool>() = Tool::Inspect; |
| 299 | *world.resource_mut::<Selected>() = Selected(None); |
| 300 | *world.resource_mut::<FrontRuntime>() = FrontRuntime::default(); |
| 301 | *world.resource_mut::<TrainCounter>() = TrainCounter::default(); |
| 302 | *world.resource_mut::<SeasonState>() = SeasonState::default(); |
| 303 | let trains: Vec<Entity> = world |
| 304 | .query_filtered::<Entity, With<Train>>() |
| 305 | .iter(world) |
| 306 | .collect(); |
| 307 | for e in trains { |
| 308 | world.entity_mut(e).despawn(); |
| 309 | } |
| 310 | world.spawn(crate::trains::starting_train()); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | fn outcome_overlay( |
| 315 | mut commands: Commands, |
| 316 | outcome: Res<Outcome>, |
| 317 | clock: Res<SimClock>, |
| 318 | existing: Query<Entity, With<OutcomeOverlay>>, |
| 319 | ) { |
| 320 | let (title, accent) = match *outcome { |
| 321 | Outcome::Ongoing => { |
| 322 | for e in &existing { |
| 323 | commands.entity(e).despawn(); |
| 324 | } |
| 325 | return; |
| 326 | } |
| 327 | Outcome::Victory => ("OPERATION SUCCESSFUL", Color::srgb(0.72, 0.84, 0.46)), |
| 328 | Outcome::Defeat => ("THE FRONT HAS COLLAPSED", Color::srgb(0.92, 0.28, 0.22)), |
| 329 | }; |
| 330 | if !existing.is_empty() { |
| 331 | return; |
| 332 | } |
| 333 | let paper = Color::srgb(0.90, 0.85, 0.70); |
| 334 | let day = format!("Day {}", clock.day() + 1); |
| 335 | commands.spawn(( |
| 336 | OutcomeOverlay, |
| 337 | GlobalZIndex(100), |
| 338 | Node { |
| 339 | position_type: PositionType::Absolute, |
| 340 | width: Val::Percent(100.0), |
| 341 | height: Val::Percent(100.0), |
| 342 | justify_content: JustifyContent::Center, |
| 343 | align_items: AlignItems::Center, |
| 344 | ..default() |
| 345 | }, |
| 346 | BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.6)), |
| 347 | children![( |
| 348 | Node { |
| 349 | flex_direction: FlexDirection::Column, |
| 350 | align_items: AlignItems::Center, |
| 351 | row_gap: Val::Px(16.0), |
| 352 | padding: UiRect::all(Val::Px(36.0)), |
| 353 | border: UiRect::all(Val::Px(4.0)), |
| 354 | min_width: Val::Px(560.0), |
| 355 | ..default() |
| 356 | }, |
| 357 | BackgroundColor(Color::srgb(0.20, 0.21, 0.15)), |
| 358 | BorderColor::all(accent), |
| 359 | children![ |
| 360 | ( |
| 361 | Text::new("FRONT COMMAND - OPERATIONS REPORT"), |
| 362 | TextFont { |
| 363 | font_size: FontSize::Px(18.0), |
| 364 | ..default() |
| 365 | }, |
| 366 | TextColor(paper), |
| 367 | ), |
| 368 | ( |
| 369 | Text::new(title), |
| 370 | TextFont { |
| 371 | font_size: FontSize::Px(44.0), |
| 372 | ..default() |
| 373 | }, |
| 374 | TextColor(accent), |
| 375 | ), |
| 376 | ( |
| 377 | Text::new(day), |
| 378 | TextFont { |
| 379 | font_size: FontSize::Px(24.0), |
| 380 | ..default() |
| 381 | }, |
| 382 | TextColor(paper), |
| 383 | ), |
| 384 | ( |
| 385 | Text::new("Press Enter to restart"), |
| 386 | TextFont { |
| 387 | font_size: FontSize::Px(20.0), |
| 388 | ..default() |
| 389 | }, |
| 390 | TextColor(paper), |
| 391 | ), |
| 392 | ] |
| 393 | )], |
| 394 | )); |
| 395 | } |
| 396 | |
| 397 | fn front_red() -> Color { |
| 398 | Color::srgb(0.78, 0.12, 0.10) |
| 399 | } |
| 400 | |
| 401 | |
| 402 | fn supply_color(t: f32) -> Color { |
| 403 | let red = Vec3::new(0.85, 0.15, 0.10); |
| 404 | let green = Vec3::new(0.20, 0.75, 0.25); |
| 405 | let c = red.lerp(green, t.clamp(0.0, 1.0)); |
| 406 | Color::srgb(c.x, c.y, c.z) |
| 407 | } |
| 408 | |
| 409 | |
| 410 | fn front_base_line(net: &Network) -> Vec<Vec2> { |
| 411 | let mut sectors: Vec<Vec2> = net |
| 412 | .nodes |
| 413 | .iter() |
| 414 | .filter_map(|n| { |
| 415 | n.front |
| 416 | .map(|f| Vec2::new(n.pos.x + LINE_OFFSET_X + f.line * LINE_SWING_X, n.pos.y)) |
| 417 | }) |
| 418 | .collect(); |
| 419 | if sectors.is_empty() { |
| 420 | return sectors; |
| 421 | } |
| 422 | sectors.sort_by(|a, b| b.y.total_cmp(&a.y)); |
| 423 | let north = sectors[0]; |
| 424 | let south = sectors[sectors.len() - 1]; |
| 425 | let mut out = vec![Vec2::new(north.x, LINE_EXTENT_Y)]; |
| 426 | out.extend(sectors); |
| 427 | out.push(Vec2::new(south.x, -LINE_EXTENT_Y)); |
| 428 | out |
| 429 | } |
| 430 | |
| 431 | |
| 432 | fn jagged(base: &[Vec2]) -> Vec<Vec2> { |
| 433 | let mut out = Vec::new(); |
| 434 | for w in base.windows(2) { |
| 435 | let (a, b) = (w[0], w[1]); |
| 436 | let steps = ((a.y - b.y).abs() / JAG_STEP).ceil().max(1.0) as usize; |
| 437 | for s in 0..steps { |
| 438 | let p = a.lerp(b, s as f32 / steps as f32); |
| 439 | let sign = if s % 2 == 0 { -1.0 } else { 1.0 }; |
| 440 | out.push(Vec2::new(p.x + sign * JAG_AMPLITUDE, p.y)); |
| 441 | } |
| 442 | } |
| 443 | if let Some(&last) = base.last() { |
| 444 | out.push(last); |
| 445 | } |
| 446 | out |
| 447 | } |
| 448 | |
| 449 | |
| 450 | fn x_at(base: &[Vec2], y: f32) -> f32 { |
| 451 | for w in base.windows(2) { |
| 452 | let (a, b) = (w[0], w[1]); |
| 453 | if y <= a.y && y >= b.y { |
| 454 | let span = a.y - b.y; |
| 455 | if span <= 0.0 { |
| 456 | return a.x; |
| 457 | } |
| 458 | return a.x + (b.x - a.x) * ((a.y - y) / span); |
| 459 | } |
| 460 | } |
| 461 | base.first().map_or(0.0, |p| p.x) |
| 462 | } |
| 463 | |
| 464 | fn draw_line(gizmos: &mut Gizmos, base: &[Vec2]) { |
| 465 | let pts = jagged(base); |
| 466 | |
| 467 | for dx in [-1.5_f32, -0.5, 0.5, 1.5] { |
| 468 | gizmos.linestrip_2d(pts.iter().map(|p| *p + Vec2::new(dx, 0.0)), front_red()); |
| 469 | } |
| 470 | |
| 471 | let mut to_next = TOOTH_SPACING; |
| 472 | for w in pts.windows(2) { |
| 473 | let seg = w[1] - w[0]; |
| 474 | let len = seg.length(); |
| 475 | if len <= f32::EPSILON { |
| 476 | continue; |
| 477 | } |
| 478 | let dir = seg / len; |
| 479 | let mut normal = Vec2::new(-dir.y, dir.x); |
| 480 | if normal.x < 0.0 { |
| 481 | normal = -normal; |
| 482 | } |
| 483 | let mut along = to_next; |
| 484 | while along <= len { |
| 485 | let p = w[0] + dir * along; |
| 486 | gizmos.line_2d(p, p + normal * TOOTH_LEN, front_red()); |
| 487 | along += TOOTH_SPACING; |
| 488 | } |
| 489 | to_next = along - len; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | |
| 494 | fn draw_hatching(gizmos: &mut Gizmos, base: &[Vec2]) { |
| 495 | let color = Color::srgba(0.78, 0.12, 0.10, 0.22); |
| 496 | let mut y = -LINE_EXTENT_Y; |
| 497 | while y <= LINE_EXTENT_Y { |
| 498 | let x = x_at(base, y); |
| 499 | gizmos.line_2d( |
| 500 | Vec2::new(x, y), |
| 501 | Vec2::new(x + HATCH_LEN, y + HATCH_LEN), |
| 502 | color, |
| 503 | ); |
| 504 | y += HATCH_STEP; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
| 509 | fn draw_supply_bar(gizmos: &mut Gizmos, pos: Vec2, supply: f32) { |
| 510 | let left = pos + Vec2::new(-BAR_W * 0.5, BAR_DY); |
| 511 | let frac = supply.clamp(0.0, 1.0); |
| 512 | let frame = Color::srgb(0.12, 0.11, 0.09); |
| 513 | gizmos.linestrip_2d( |
| 514 | [ |
| 515 | left, |
| 516 | left + Vec2::new(BAR_W, 0.0), |
| 517 | left + Vec2::new(BAR_W, BAR_H), |
| 518 | left + Vec2::new(0.0, BAR_H), |
| 519 | left, |
| 520 | ], |
| 521 | frame, |
| 522 | ); |
| 523 | let fill = supply_color(frac); |
| 524 | for row in 0..=(BAR_H as u32) { |
| 525 | let y = left.y + row as f32; |
| 526 | gizmos.line_2d( |
| 527 | Vec2::new(left.x, y), |
| 528 | Vec2::new(left.x + BAR_W * frac, y), |
| 529 | fill, |
| 530 | ); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
| 535 | const BOMBER: [&[[f32; 2]]; 5] = [ |
| 536 | &[[22.0, 0.0], [15.0, -2.5], [-14.0, -2.5], [-20.0, -1.0], [-20.0, 1.0], [-14.0, 2.5], [15.0, 2.5]], |
| 537 | &[[8.0, 2.5], [-3.0, 19.0], [-9.0, 19.0], [-6.0, 2.5]], |
| 538 | &[[8.0, -2.5], [-3.0, -19.0], [-9.0, -19.0], [-6.0, -2.5]], |
| 539 | &[[-15.0, 1.5], [-20.0, 9.0], [-24.0, 9.0], [-21.0, 1.0]], |
| 540 | &[[-15.0, -1.5], [-20.0, -9.0], [-24.0, -9.0], [-21.0, -1.0]], |
| 541 | ]; |
| 542 | |
| 543 | const FIGHTER: [&[[f32; 2]]; 3] = [ |
| 544 | &[[16.0, 0.0], [8.0, -1.5], [-10.0, -1.5], [-12.0, 0.0], [-10.0, 1.5], [8.0, 1.5]], |
| 545 | &[[3.0, 1.5], [-3.0, 11.0], [-6.0, 11.0], [-5.0, 1.5]], |
| 546 | &[[3.0, -1.5], [-3.0, -11.0], [-6.0, -11.0], [-5.0, -1.5]], |
| 547 | ]; |
| 548 | |
| 549 | |
| 550 | fn draw_plane( |
| 551 | gizmos: &mut Gizmos, |
| 552 | center: Vec2, |
| 553 | heading: Vec2, |
| 554 | scale: f32, |
| 555 | parts: &[&[[f32; 2]]], |
| 556 | color: Color, |
| 557 | ) { |
| 558 | let side = Vec2::new(-heading.y, heading.x); |
| 559 | for part in parts { |
| 560 | let pts: Vec<Vec2> = part |
| 561 | .iter() |
| 562 | .map(|p| center + heading * (p[0] * scale) + side * (p[1] * scale)) |
| 563 | .collect(); |
| 564 | |
| 565 | for off in [-0.6_f32, 0.0, 0.6] { |
| 566 | let o = side * off; |
| 567 | gizmos.linestrip_2d( |
| 568 | pts.iter().map(|p| *p + o).chain(std::iter::once(pts[0] + o)), |
| 569 | color, |
| 570 | ); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | fn fill_disc(gizmos: &mut Gizmos, c: Vec2, radius: f32, color: Color) { |
| 577 | let mut r = radius; |
| 578 | while r > 0.5 { |
| 579 | gizmos.circle_2d(c, r, color); |
| 580 | r -= 1.5; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | |
| 585 | fn draw_raid(gizmos: &mut Gizmos, fx: &RaidFx) { |
| 586 | let t = (fx.age / RAID_FX_SECS).clamp(0.0, 1.0); |
| 587 | let dir = (fx.b - fx.a).normalize_or_zero(); |
| 588 | let perp = Vec2::new(-dir.y, dir.x); |
| 589 | |
| 590 | |
| 591 | if fx.age >= RAID_FLASH_START { |
| 592 | let fade = (1.0 - (fx.age - RAID_FLASH_START) / 1.5).clamp(0.0, 1.0); |
| 593 | let blink = 0.5 + 0.5 * (fx.age * 16.0).sin(); |
| 594 | let color = Color::srgba(1.0, 0.32, 0.06, fade * (0.35 + 0.65 * blink)); |
| 595 | for s in [-3.0_f32, -1.5, 0.0, 1.5, 3.0] { |
| 596 | gizmos.line_2d(fx.a + perp * s, fx.b + perp * s, color); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | let mid = (fx.a + fx.b) * 0.5; |
| 602 | let from = mid + RAID_PLANE_REACH; |
| 603 | let to = mid - RAID_PLANE_REACH; |
| 604 | let pos = from.lerp(to, t); |
| 605 | let heading = (to - from).normalize_or_zero(); |
| 606 | let alpha = (t / 0.08).min(1.0) * ((1.0 - t) / 0.1).min(1.0); |
| 607 | let ink = Color::srgba(0.10, 0.08, 0.07, alpha); |
| 608 | draw_plane(gizmos, pos, heading, 1.0, &BOMBER, ink); |
| 609 | draw_plane(gizmos, pos + heading * 70.0, heading, 0.55, &FIGHTER, ink); |
| 610 | |
| 611 | |
| 612 | for (start, along) in RAID_BURSTS { |
| 613 | let local = fx.age - start; |
| 614 | if !(0.0..RAID_BURST_SECS).contains(&local) { |
| 615 | continue; |
| 616 | } |
| 617 | let u = local / RAID_BURST_SECS; |
| 618 | let fade = 1.0 - u; |
| 619 | let c = fx.a.lerp(fx.b, along); |
| 620 | fill_disc(gizmos, c, 4.0 + 9.0 * fade, Color::srgba(1.0, 0.85, 0.3, fade)); |
| 621 | gizmos.circle_2d(c, 6.0 + 30.0 * u, Color::srgba(1.0, 0.5, 0.1, fade)); |
| 622 | gizmos.circle_2d(c, 4.0 + 20.0 * u, Color::srgba(0.05, 0.05, 0.05, fade)); |
| 623 | for k in 0..8 { |
| 624 | let ang = k as f32 * std::f32::consts::FRAC_PI_4 + 0.3; |
| 625 | let u2 = Vec2::new(ang.cos(), ang.sin()); |
| 626 | gizmos.line_2d( |
| 627 | c + u2 * (8.0 + 14.0 * u), |
| 628 | c + u2 * (12.0 + 26.0 * u), |
| 629 | Color::srgba(1.0, 0.7, 0.2, fade), |
| 630 | ); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | fn draw_front( |
| 636 | mut gizmos: Gizmos, |
| 637 | net: Res<Network>, |
| 638 | time: Res<Time<Real>>, |
| 639 | mut runtime: ResMut<FrontRuntime>, |
| 640 | ) { |
| 641 | let base = front_base_line(&net); |
| 642 | if !base.is_empty() { |
| 643 | draw_hatching(&mut gizmos, &base); |
| 644 | draw_line(&mut gizmos, &base); |
| 645 | } |
| 646 | for node in net.nodes.iter() { |
| 647 | if let Some(f) = node.front { |
| 648 | draw_supply_bar(&mut gizmos, node.pos, f.supply); |
| 649 | } |
| 650 | } |
| 651 | let dt = time.delta_secs(); |
| 652 | runtime.raids.retain_mut(|r| { |
| 653 | r.age += dt; |
| 654 | r.age < RAID_FX_SECS |
| 655 | }); |
| 656 | for fx in &runtime.raids { |
| 657 | draw_raid(&mut gizmos, fx); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | #[cfg(test)] |
| 662 | mod tests { |
| 663 | use super::*; |
| 664 | |
| 665 | fn front(line: f32, supply: f32) -> FrontState { |
| 666 | FrontState { line, supply } |
| 667 | } |
| 668 | |
| 669 | fn close(a: f32, b: f32) -> bool { |
| 670 | (a - b).abs() < 1e-5 |
| 671 | } |
| 672 | |
| 673 | #[test] |
| 674 | fn fully_supplied_sector_consumes_and_gains_supply() { |
| 675 | let (stock, f) = step_sector([15.0, 15.0, 15.0, 0.0], front(0.0, 0.5), 1.0); |
| 676 | assert!(close(stock[0], 14.0)); |
| 677 | assert!(close(stock[1], 14.2)); |
| 678 | assert!(close(stock[2], 14.85)); |
| 679 | assert!(close(f.supply, 0.55)); |
| 680 | assert!(close(f.line, (0.55 - NEUTRAL_SUPPLY) * LINE_RATE)); |
| 681 | assert!(close(stock[3], 0.2 + (1.0 - 0.55) * 0.5)); |
| 682 | } |
| 683 | |
| 684 | #[test] |
| 685 | fn self_repair_adds_one_percent_and_caps_at_intact() { |
| 686 | assert!(close(repair_step(0.5), 0.51)); |
| 687 | assert!(close(repair_step(0.995), 1.0)); |
| 688 | assert!(close(repair_step(1.0), 1.0)); |
| 689 | } |
| 690 | } |