2021-12-10 20:16:48 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-11-03 09:55:17 -04:00
|
|
|
use rltk::{RandomNumberGenerator, RGB};
|
|
|
|
use specs::prelude::*;
|
2021-11-08 13:58:40 -05:00
|
|
|
use specs::saveload::{MarkedBuilder, SimpleMarker};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
|
|
|
use crate::components::*;
|
|
|
|
use crate::map::MAP_WIDTH;
|
|
|
|
use crate::random_table::RandomTable;
|
|
|
|
use crate::{Map, Rect, TileType};
|
2021-11-03 09:55:17 -04:00
|
|
|
|
|
|
|
/// Spawns the player and returns their entity object
|
|
|
|
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position {
|
|
|
|
x: player_x,
|
|
|
|
y: player_y,
|
|
|
|
})
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('@'),
|
|
|
|
fg: RGB::named(rltk::YELLOW),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
2021-11-04 09:54:38 -04:00
|
|
|
render_order: 0,
|
2021-11-03 09:55:17 -04:00
|
|
|
})
|
|
|
|
.with(Player {})
|
2021-11-05 10:41:47 -04:00
|
|
|
.with(Viewshed::default())
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Player"))
|
2021-11-03 09:55:17 -04:00
|
|
|
.with(CombatStats {
|
|
|
|
max_hp: 30,
|
|
|
|
hp: 30,
|
|
|
|
defense: 2,
|
|
|
|
power: 5,
|
|
|
|
})
|
2021-11-17 16:23:01 -05:00
|
|
|
.with(HungerClock {
|
|
|
|
state: HungerState::WellFed,
|
|
|
|
duration: 20,
|
|
|
|
})
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-03 09:55:17 -04:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:06:55 -05:00
|
|
|
const MAX_MONSTERS: i32 = 4;
|
|
|
|
|
2021-11-12 14:12:15 -05:00
|
|
|
fn room_table(map_depth: i32) -> RandomTable {
|
2021-11-12 14:06:55 -05:00
|
|
|
RandomTable::new()
|
|
|
|
.add("Goblin", 10)
|
2021-11-12 14:12:15 -05:00
|
|
|
.add("Orc", 1 + map_depth)
|
2021-11-12 14:06:55 -05:00
|
|
|
.add("Health Potion", 7)
|
2021-11-12 14:12:15 -05:00
|
|
|
.add("Fireball Scroll", 2 + map_depth)
|
|
|
|
.add("Confusion Scroll", 2 + map_depth)
|
2021-11-12 14:06:55 -05:00
|
|
|
.add("Magic Missile Scroll", 4)
|
2021-11-15 09:19:22 -05:00
|
|
|
.add("Dagger", 3)
|
|
|
|
.add("Shield", 3)
|
2021-11-15 11:48:01 -05:00
|
|
|
.add("Longsword", map_depth - 1)
|
|
|
|
.add("Tower Shield", map_depth - 1)
|
2021-11-18 15:25:29 -05:00
|
|
|
.add("Rations", 10)
|
2021-11-29 14:39:15 -05:00
|
|
|
.add("Magic Mapping Scroll", 2)
|
2021-11-29 16:00:07 -05:00
|
|
|
.add("Bear Trap", 2)
|
2021-11-12 14:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// fills a room with stuff!
|
|
|
|
#[allow(clippy::map_entry)]
|
2021-12-10 16:34:11 -05:00
|
|
|
pub fn spawn_room(
|
|
|
|
map: &Map,
|
|
|
|
rng: &mut RandomNumberGenerator,
|
|
|
|
room: &Rect,
|
|
|
|
map_depth: i32,
|
|
|
|
spawn_list: &mut Vec<(usize, String)>,
|
|
|
|
) {
|
2021-12-03 15:55:07 -05:00
|
|
|
let mut possible_targets: Vec<usize> = Vec::new();
|
|
|
|
|
|
|
|
// Borrow scope - to keep access to the map separated
|
|
|
|
{
|
|
|
|
for y in room.y1 + 1..room.y2 {
|
|
|
|
for x in room.x1 + 1..room.x2 {
|
|
|
|
let idx = map.xy_idx(x, y);
|
|
|
|
if map.tiles[idx] == TileType::Floor {
|
|
|
|
possible_targets.push(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:34:11 -05:00
|
|
|
spawn_region(map, rng, &possible_targets, map_depth, spawn_list);
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
|
2021-12-10 16:34:11 -05:00
|
|
|
pub fn spawn_region(
|
2021-12-14 14:15:22 -05:00
|
|
|
_map: &Map,
|
2021-12-10 16:34:11 -05:00
|
|
|
rng: &mut RandomNumberGenerator,
|
|
|
|
area: &[usize],
|
|
|
|
map_depth: i32,
|
|
|
|
spawn_list: &mut Vec<(usize, String)>,
|
|
|
|
) {
|
2021-11-12 14:12:15 -05:00
|
|
|
let spawn_table = room_table(map_depth);
|
2021-11-12 14:06:55 -05:00
|
|
|
let mut spawn_points: HashMap<usize, String> = HashMap::new();
|
2021-12-03 15:55:07 -05:00
|
|
|
let mut areas: Vec<usize> = Vec::from(area);
|
2021-11-03 09:55:17 -04:00
|
|
|
|
2021-11-12 14:06:55 -05:00
|
|
|
// Scope to keep the borrow checker happy
|
2021-11-03 09:55:17 -04:00
|
|
|
{
|
2021-12-03 15:55:07 -05:00
|
|
|
let num_spawns = i32::min(
|
|
|
|
areas.len() as i32,
|
|
|
|
rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3,
|
|
|
|
);
|
|
|
|
if num_spawns == 0 {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-12 14:06:55 -05:00
|
|
|
|
|
|
|
for _i in 0..num_spawns {
|
2021-12-03 15:55:07 -05:00
|
|
|
let array_index = if areas.len() == 1 {
|
2021-12-10 16:34:11 -05:00
|
|
|
0_usize
|
2021-12-03 15:55:07 -05:00
|
|
|
} else {
|
|
|
|
(rng.roll_dice(1, areas.len() as i32) - 1) as usize
|
|
|
|
};
|
|
|
|
let map_idx = areas[array_index];
|
2021-12-10 16:34:11 -05:00
|
|
|
spawn_points.insert(map_idx, spawn_table.roll(rng));
|
2021-12-03 15:55:07 -05:00
|
|
|
areas.remove(array_index);
|
2021-11-12 14:06:55 -05:00
|
|
|
}
|
2021-11-03 09:55:17 -04:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:55:07 -05:00
|
|
|
// Actually spawn the monsters
|
2021-11-12 14:06:55 -05:00
|
|
|
for spawn in spawn_points.iter() {
|
2021-12-10 16:34:11 -05:00
|
|
|
spawn_list.push((*spawn.0, spawn.1.to_string()));
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawns a named entity (name in tuple.1) at the location in (tuple.0)
|
2021-12-10 14:29:03 -05:00
|
|
|
pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) {
|
2021-12-03 15:55:07 -05:00
|
|
|
let x = (*spawn.0 % MAP_WIDTH) as i32;
|
|
|
|
let y = (*spawn.0 / MAP_WIDTH) as i32;
|
|
|
|
|
|
|
|
match spawn.1.as_ref() {
|
|
|
|
"Goblin" => goblin(ecs, x, y),
|
|
|
|
"Orc" => orc(ecs, x, y),
|
|
|
|
"Health Potion" => health_potion(ecs, x, y),
|
|
|
|
"Fireball Scroll" => fireball_scroll(ecs, x, y),
|
|
|
|
"Confusion Scroll" => confusion_scroll(ecs, x, y),
|
|
|
|
"Magic Missile Scroll" => magic_missile_scroll(ecs, x, y),
|
|
|
|
"Dagger" => dagger(ecs, x, y),
|
|
|
|
"Shield" => shield(ecs, x, y),
|
|
|
|
"Longsword" => longsword(ecs, x, y),
|
|
|
|
"Tower Shield" => tower_shield(ecs, x, y),
|
|
|
|
"Rations" => rations(ecs, x, y),
|
|
|
|
"Magic Mapping Scroll" => magic_mapping_scroll(ecs, x, y),
|
|
|
|
"Bear Trap" => bear_trap(ecs, x, y),
|
2021-12-17 13:53:14 -05:00
|
|
|
"Door" => door(ecs, x, y),
|
2021-12-03 15:55:07 -05:00
|
|
|
_ => {}
|
2021-11-03 09:55:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn orc(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
monster(ecs, x, y, rltk::to_cp437('o'), "Orc");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn goblin(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
monster(ecs, x, y, rltk::to_cp437('g'), "Goblin");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharType, name: S) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph,
|
|
|
|
fg: RGB::named(rltk::RED),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
2021-11-04 09:54:38 -04:00
|
|
|
render_order: 1,
|
2021-11-03 09:55:17 -04:00
|
|
|
})
|
2021-11-05 10:41:47 -04:00
|
|
|
.with(Viewshed::default())
|
2021-11-03 09:55:17 -04:00
|
|
|
.with(Monster {})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from(name))
|
2021-11-03 09:55:17 -04:00
|
|
|
.with(BlocksTile {})
|
|
|
|
.with(CombatStats {
|
|
|
|
max_hp: 16,
|
|
|
|
hp: 16,
|
|
|
|
defense: 1,
|
|
|
|
power: 4,
|
|
|
|
})
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-03 09:55:17 -04:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2021-11-03 15:11:19 -04:00
|
|
|
fn health_potion(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('¡'),
|
|
|
|
fg: RGB::named(rltk::MAGENTA),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
2021-11-04 09:54:38 -04:00
|
|
|
render_order: 2,
|
2021-11-03 15:11:19 -04:00
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Health Potion"))
|
2021-11-03 15:11:19 -04:00
|
|
|
.with(Item {})
|
2021-11-04 15:06:04 -04:00
|
|
|
.with(Consumable {})
|
|
|
|
.with(ProvidesHealing { heal_amount: 8 })
|
2021-11-15 13:55:31 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-03 15:11:19 -04:00
|
|
|
.build();
|
2021-11-03 09:55:17 -04:00
|
|
|
}
|
2021-11-05 10:42:44 -04:00
|
|
|
|
|
|
|
fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Magic Missile Scroll"))
|
2021-11-05 10:42:44 -04:00
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
2021-11-15 13:55:31 -05:00
|
|
|
.with(InflictsDamage { damage: 20 })
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-05 10:42:44 -04:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:12:22 -04:00
|
|
|
fn fireball_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
|
|
|
fg: RGB::named(rltk::ORANGE),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Fireball Scroll"))
|
2021-11-05 13:12:22 -04:00
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
|
|
|
.with(InflictsDamage { damage: 20 })
|
|
|
|
.with(AreaOfEffect { radius: 3 })
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-05 13:12:22 -04:00
|
|
|
.build();
|
|
|
|
}
|
2021-11-05 14:32:14 -04:00
|
|
|
|
|
|
|
fn confusion_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
|
|
|
fg: RGB::named(rltk::PINK),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Confusion Scroll"))
|
2021-11-05 14:32:14 -04:00
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
|
|
|
.with(Confusion { turns: 4 })
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2021-11-05 14:32:14 -04:00
|
|
|
.build();
|
|
|
|
}
|
2021-11-15 09:19:22 -05:00
|
|
|
|
|
|
|
fn dagger(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('/'),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Dagger"))
|
2021-11-15 09:19:22 -05:00
|
|
|
.with(Item {})
|
|
|
|
.with(Equippable {
|
|
|
|
slot: EquipmentSlot::Melee,
|
|
|
|
})
|
2021-11-15 09:45:12 -05:00
|
|
|
.with(MeleePowerBonus { power: 2 })
|
2021-11-15 09:19:22 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shield(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('('),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Shield"))
|
2021-11-15 09:19:22 -05:00
|
|
|
.with(Item {})
|
|
|
|
.with(Equippable {
|
|
|
|
slot: EquipmentSlot::Shield,
|
|
|
|
})
|
2021-11-15 09:45:12 -05:00
|
|
|
.with(DefenseBonus { defense: 1 })
|
2021-11-15 09:19:22 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
2021-11-15 11:48:01 -05:00
|
|
|
|
|
|
|
fn longsword(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('/'),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Longsword"))
|
2021-11-15 11:48:01 -05:00
|
|
|
.with(Item {})
|
|
|
|
.with(Equippable {
|
|
|
|
slot: EquipmentSlot::Melee,
|
|
|
|
})
|
|
|
|
.with(MeleePowerBonus { power: 4 })
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tower_shield(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('('),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
2021-11-18 15:25:29 -05:00
|
|
|
.with(Name::from("Tower Shield"))
|
2021-11-15 11:48:01 -05:00
|
|
|
.with(Item {})
|
|
|
|
.with(Equippable {
|
|
|
|
slot: EquipmentSlot::Shield,
|
|
|
|
})
|
|
|
|
.with(DefenseBonus { defense: 3 })
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
2021-11-15 13:27:40 -05:00
|
|
|
}
|
2021-11-18 15:25:29 -05:00
|
|
|
|
|
|
|
fn rations(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('%'),
|
|
|
|
fg: RGB::named(rltk::GREEN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name::from("Rations"))
|
|
|
|
.with(Item {})
|
|
|
|
.with(ProvidesFood {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
2021-11-29 14:39:15 -05:00
|
|
|
|
|
|
|
fn magic_mapping_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
|
|
|
fg: RGB::named(rltk::CYAN3),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name::from("Scroll of Magic Mapping"))
|
|
|
|
.with(Item {})
|
|
|
|
.with(MagicMapper {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
2021-11-29 16:00:07 -05:00
|
|
|
|
|
|
|
fn bear_trap(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('^'),
|
|
|
|
fg: RGB::named(rltk::RED),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name::from("Bear Trap"))
|
|
|
|
.with(Hidden {})
|
|
|
|
.with(EntryTrigger {})
|
|
|
|
.with(InflictsDamage { damage: 6 })
|
2021-12-01 09:46:34 -05:00
|
|
|
.with(SingleActivation {})
|
2021-11-29 16:00:07 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
2021-12-17 13:53:14 -05:00
|
|
|
|
|
|
|
fn door(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('+'),
|
|
|
|
fg: RGB::named(rltk::CHOCOLATE),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name::from("Door"))
|
2021-12-17 14:07:14 -05:00
|
|
|
.with(BlocksTile {})
|
|
|
|
.with(BlocksVisibility {})
|
|
|
|
.with(Door { open: false })
|
2021-12-17 13:53:14 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|