use std::cmp::{max, min}; use rltk::{DistanceAlg, Point, RandomNumberGenerator, Rltk, VirtualKeyCode}; use specs::prelude::*; use crate::components::{ Attributes, BlocksTile, BlocksVisibility, Door, EntityMoved, Equipped, Faction, HungerClock, HungerState, Item, Name, Player, Pools, Position, Renderable, Target, Vendor, Viewshed, WantsToMelee, WantsToPickupItem, WantsToShoot, }; use crate::raws::{self, Reaction, RAWS}; use crate::{ colors, gamelog, spatial, Map, RunState, State, TileType, VendorMode, WantsToCastSpell, Weapon, }; fn get_player_target_list(ecs: &mut World) -> Vec<(f32, Entity)> { let mut possible_targets = Vec::new(); let viewsheds = ecs.read_storage::(); let player_entity = ecs.fetch::(); let equipped = ecs.read_storage::(); let weapon = ecs.read_storage::(); let map = ecs.fetch::(); let positions = ecs.read_storage::(); let factions = ecs.read_storage::(); for (equipped, weapon) in (&equipped, &weapon).join() { if equipped.owner == *player_entity { if let Some(range) = weapon.range { if let Some(vs) = viewsheds.get(*player_entity) { let player_pos = positions.get(*player_entity).unwrap(); for tile_point in vs.visible_tiles.iter() { let tile_idx = map.xy_idx(tile_point.x, tile_point.y); let distance_to_target = DistanceAlg::Pythagoras .distance2d(*tile_point, Point::from(*player_pos)); if distance_to_target < range as f32 { spatial::for_each_tile_content(tile_idx, |possible_target| { if possible_target != *player_entity && factions.get(possible_target).is_some() { possible_targets.push((distance_to_target, possible_target)); } }); } } } } } } possible_targets.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); possible_targets } pub fn end_turn_targeting(ecs: &mut World) { let possible_targets = get_player_target_list(ecs); let mut targets = ecs.write_storage::(); targets.clear(); if !possible_targets.is_empty() { targets .insert(possible_targets[0].1, Target {}) .expect("Failed to insert Target tag"); } } fn fire_on_target(ecs: &mut World) -> RunState { let targets = ecs.write_storage::(); let entities = ecs.entities(); let mut current_target: Option = None; for (e, _t) in (&entities, &targets).join() { current_target = Some(e); } if let Some(target) = current_target { let player_entity = ecs.fetch::(); let mut shoot_store = ecs.write_storage::(); let names = ecs.read_storage::(); if let Some(name) = names.get(target) { gamelog::line("You fire at") .append_color(colors::CYAN, &name.name) .log(); } shoot_store .insert(*player_entity, WantsToShoot { target }) .expect("Insert Fail"); RunState::Ticking } else { gamelog::log_line("You don't have a target selected!"); RunState::AwaitingInput } } fn cycle_target(ecs: &mut World) { let possible_targets = get_player_target_list(ecs); let mut targets = ecs.write_storage::(); let entities = ecs.entities(); let mut current_target: Option = None; for (e, _t) in (&entities, &targets).join() { current_target = Some(e); } targets.clear(); if let Some(current_target) = current_target { if !possible_targets.len() > 1 { let mut index = 0; for (i, target) in possible_targets.iter().enumerate() { if target.1 == current_target { index = i; } } if index > possible_targets.len() - 2 { targets .insert(possible_targets[0].1, Target {}) .expect("Failed to insert Target tag"); } else { targets .insert(possible_targets[index + 1].1, Target {}) .expect("Failed to insert Target tag"); } } } } pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState { let mut positions = ecs.write_storage::(); let players = ecs.read_storage::(); let mut viewsheds = ecs.write_storage::(); let entities = ecs.entities(); let combat_stats = ecs.read_storage::(); let map = ecs.fetch::(); let mut wants_to_melee = ecs.write_storage::(); let mut entity_moved = ecs.write_storage::(); let mut doors = ecs.write_storage::(); let mut blocks_visibility = ecs.write_storage::(); let mut blocks_movement = ecs.write_storage::(); let mut renderables = ecs.write_storage::(); let factions = ecs.read_storage::(); let vendors = ecs.read_storage::(); let mut result = RunState::AwaitingInput; let mut swap_entities: Vec<(Entity, i32, i32)> = Vec::new(); for (entity, _player, pos, viewshed) in (&entities, &players, &mut positions, &mut viewsheds).join() { if pos.x + delta_x < 1 || pos.x + delta_x > map.width - 1 || pos.y + delta_y < 1 || pos.y + delta_y > map.height - 1 { return RunState::AwaitingInput; } let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); result = spatial::for_each_tile_content_with_gamemode(destination_idx, |potential_target| { if vendors.get(potential_target).is_some() { return Some(RunState::ShowVendor { vendor: potential_target, mode: VendorMode::Sell, }); } let mut hostile = true; if combat_stats.get(potential_target).is_some() { if let Some(faction) = factions.get(potential_target) { let reaction = crate::raws::faction_reaction( &faction.name, "Player", &RAWS.lock().unwrap(), ); if reaction != Reaction::Attack { hostile = false; } } } if !hostile { // Note that we want to move the bystander swap_entities.push((potential_target, pos.x, pos.y)); // Move the player pos.x = min(map.width - 1, max(0, pos.x + delta_x)); pos.y = min(map.height - 1, max(0, pos.y + delta_y)); entity_moved .insert(entity, EntityMoved {}) .expect("Unable to insert marker"); viewshed.dirty = true; let mut ppos = ecs.write_resource::(); ppos.x = pos.x; ppos.y = pos.y; return Some(RunState::Ticking); } else if combat_stats.get(potential_target).is_some() { wants_to_melee .insert( entity, WantsToMelee { target: potential_target, }, ) .expect("Add target failed"); return Some(RunState::Ticking); } if let Some(door) = doors.get_mut(potential_target) { door.open = true; blocks_visibility.remove(potential_target); blocks_movement.remove(potential_target); let glyph = renderables.get_mut(potential_target).unwrap(); glyph.glyph = rltk::to_cp437('/'); viewshed.dirty = true; return Some(RunState::Ticking); } None }); if !spatial::is_blocked(destination_idx) { let old_idx = map.xy_idx(pos.x, pos.y); pos.x = min(map.width - 1, max(0, pos.x + delta_x)); pos.y = min(map.height - 1, max(0, pos.y + delta_y)); let new_idx = map.xy_idx(pos.x, pos.y); entity_moved .insert(entity, EntityMoved {}) .expect("Failed to add EntityMoved flag to player"); spatial::move_entity(entity, old_idx, new_idx); viewshed.dirty = true; let mut ppos = ecs.write_resource::(); ppos.x = pos.x; ppos.y = pos.y; result = RunState::Ticking; match map.tiles[destination_idx] { TileType::DownStairs => result = RunState::NextLevel, TileType::UpStairs => result = RunState::PreviousLevel, _ => {} } } } for m in swap_entities.iter() { if let Some(their_pos) = positions.get_mut(m.0) { let old_idx = map.xy_idx(their_pos.x, their_pos.y); their_pos.x = m.1; their_pos.y = m.2; let new_idx = map.xy_idx(their_pos.x, their_pos.y); spatial::move_entity(m.0, old_idx, new_idx); result = RunState::Ticking; } } result } pub fn try_next_level(ecs: &mut World) -> bool { let player_pos = ecs.fetch::(); let map = ecs.fetch::(); let player_idx = map.xy_idx(player_pos.x, player_pos.y); if map.tiles[player_idx] == TileType::DownStairs { true } else { gamelog::log_line("There is no way down from here."); false } } pub fn try_previous_level(ecs: &mut World) -> bool { let player_pos = ecs.fetch::(); let map = ecs.fetch::(); let player_idx = map.xy_idx(player_pos.x, player_pos.y); if map.tiles[player_idx] == TileType::UpStairs { true } else { gamelog::log_line("There is no way up from here."); false } } fn get_item(ecs: &mut World) { let player_pos = ecs.fetch::(); let player_entity = ecs.fetch::(); let entities = ecs.entities(); let items = ecs.read_storage::(); let positions = ecs.read_storage::(); let mut target_item: Option = None; for (item_entity, _item, position) in (&entities, &items, &positions).join() { if position.x == player_pos.x && position.y == player_pos.y { target_item = Some(item_entity); } } match target_item { None => gamelog::log_line("There is nothing here to pick up."), Some(item) => { let mut pickup = ecs.write_storage::(); pickup .insert( *player_entity, WantsToPickupItem { collected_by: *player_entity, item, }, ) .expect("Unable to pick up item?!"); } } } fn skip_turn(ecs: &mut World) -> RunState { let player_entity = ecs.fetch::(); let viewshed_components = ecs.read_storage::(); let factions = ecs.read_storage::(); let worldmap_resource = ecs.fetch::(); let mut can_heal = true; let viewshed = viewshed_components.get(*player_entity).unwrap(); for tile in viewshed.visible_tiles.iter() { let idx = worldmap_resource.xy_idx(tile.x, tile.y); spatial::for_each_tile_content(idx, |entity_id| match factions.get(entity_id) { None => {} Some(faction) => { let reaction = raws::faction_reaction(&faction.name, "Player", &RAWS.lock().unwrap()); if reaction == Reaction::Attack { can_heal = false; } } }); } // Don't allow healing when hungry or starving let hunger_clocks = ecs.read_storage::(); if let Some(hc) = hunger_clocks.get(*player_entity) { match hc.state { HungerState::Hungry => can_heal = false, HungerState::Starving => can_heal = false, _ => {} } } if can_heal { let mut health_components = ecs.write_storage::(); let pools = health_components.get_mut(*player_entity).unwrap(); pools.hit_points.current = i32::min(pools.hit_points.current + 1, pools.hit_points.max); let mut rng = ecs.fetch_mut::(); if rng.roll_dice(1, 6) == 1 { pools.mana.current = i32::min(pools.mana.current + 1, pools.mana.max); } } RunState::Ticking } fn use_consumable_hotkey(gs: &mut State, key: i32) -> RunState { use crate::components::{Consumable, InBackpack, Ranged, WantsToUseItem}; let consumables = gs.ecs.read_storage::(); let backpack = gs.ecs.read_storage::(); let player_entity = gs.ecs.fetch::(); let entities = gs.ecs.entities(); let mut carried_consumables = Vec::new(); for (entity, carried_by, _consumable) in (&entities, &backpack, &consumables).join() { if carried_by.owner == *player_entity { carried_consumables.push(entity); } } if (key as usize) < carried_consumables.len() { if let Some(ranged) = gs .ecs .read_storage::() .get(carried_consumables[key as usize]) { return RunState::ShowTargeting { range: ranged.range, item: carried_consumables[key as usize], }; } let mut intent = gs.ecs.write_storage::(); intent .insert( *player_entity, WantsToUseItem { item: carried_consumables[key as usize], target: None, }, ) .expect("Unable to insert intent to use item."); return RunState::Ticking; } RunState::Ticking } fn use_spell_hotkey(gs: &mut State, key: i32) -> RunState { use crate::components::{KnownSpells, Ranged}; use crate::raws::find_spell_entity; let player_entity = gs.ecs.fetch::(); let known_spells_storage = gs.ecs.read_storage::(); let known_spells = &known_spells_storage.get(*player_entity).unwrap().spells; if (key as usize) < known_spells.len() { let pools = gs.ecs.read_storage::(); let player_pools = pools.get(*player_entity).unwrap(); if player_pools.mana.current >= known_spells[key as usize].mana_cost { if let Some(spell_entity) = find_spell_entity(&gs.ecs, &known_spells[key as usize].display_name) { if let Some(ranged) = gs.ecs.read_storage::().get(spell_entity) { return RunState::ShowTargeting { range: ranged.range, item: spell_entity, }; } let mut intent = gs.ecs.write_storage::(); intent .insert( *player_entity, WantsToCastSpell { spell: spell_entity, target: None, }, ) .expect("Unable to insert intent to cast spell"); return RunState::Ticking; } } else { gamelog::log_line("You don't have enough mana to cast that!"); } } RunState::Ticking } pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { // Hotkeys if (ctx.shift || ctx.control) && ctx.key.is_some() { let key: Option = match ctx.key.unwrap() { VirtualKeyCode::Key1 => Some(1), VirtualKeyCode::Key2 => Some(2), VirtualKeyCode::Key3 => Some(3), VirtualKeyCode::Key4 => Some(4), VirtualKeyCode::Key5 => Some(5), VirtualKeyCode::Key6 => Some(6), VirtualKeyCode::Key7 => Some(7), VirtualKeyCode::Key8 => Some(8), VirtualKeyCode::Key9 => Some(9), _ => None, }; if let Some(key) = key { if ctx.shift { return use_consumable_hotkey(gs, key - 1); } else if ctx.control { return use_spell_hotkey(gs, key - 1); } } } // Mac OS is special when it comes to the numpad. Instead of reporting // the keys as Numpad-specific numbers, it reports the number row scan // codes. The quick fix is to match on both types of number scan codes. match ctx.key { None => return RunState::AwaitingInput, // Nothing happened Some(key) => match key { // Cardinal directions VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::Key8 | VirtualKeyCode::K => return try_move_player(0, -1, &mut gs.ecs), VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::Key2 | VirtualKeyCode::J => return try_move_player(0, 1, &mut gs.ecs), VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::Key4 | VirtualKeyCode::H => return try_move_player(-1, 0, &mut gs.ecs), VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::Key6 | VirtualKeyCode::L => return try_move_player(1, 0, &mut gs.ecs), // Diagonals VirtualKeyCode::Numpad7 | VirtualKeyCode::Home | VirtualKeyCode::Key7 | VirtualKeyCode::U | VirtualKeyCode::Q => return try_move_player(-1, -1, &mut gs.ecs), VirtualKeyCode::Numpad9 | VirtualKeyCode::PageUp | VirtualKeyCode::Key9 | VirtualKeyCode::Y | VirtualKeyCode::W => return try_move_player(1, -1, &mut gs.ecs), VirtualKeyCode::Numpad1 | VirtualKeyCode::End | VirtualKeyCode::Key1 | VirtualKeyCode::B | VirtualKeyCode::A => return try_move_player(-1, 1, &mut gs.ecs), VirtualKeyCode::Numpad3 | VirtualKeyCode::PageDown | VirtualKeyCode::Key3 | VirtualKeyCode::N | VirtualKeyCode::S => return try_move_player(1, 1, &mut gs.ecs), // Skip Turn VirtualKeyCode::Numpad5 | VirtualKeyCode::Key5 | VirtualKeyCode::Space => { return skip_turn(&mut gs.ecs) } // Level changes VirtualKeyCode::Period => { if try_next_level(&mut gs.ecs) { return RunState::NextLevel; } } VirtualKeyCode::Comma => { if try_previous_level(&mut gs.ecs) { return RunState::PreviousLevel; } } // Item management VirtualKeyCode::G => get_item(&mut gs.ecs), VirtualKeyCode::I => return RunState::ShowInventory, VirtualKeyCode::D => return RunState::ShowDropItem, VirtualKeyCode::R => return RunState::ShowRemoveItem, // Save and Quit VirtualKeyCode::Escape => return RunState::SaveGame, // Cheating! VirtualKeyCode::Backslash => return RunState::ShowCheatMenu, // Ranged VirtualKeyCode::V => { cycle_target(&mut gs.ecs); return RunState::AwaitingInput; } VirtualKeyCode::F => return fire_on_target(&mut gs.ecs), _ => return RunState::AwaitingInput, }, } RunState::Ticking }