2022-02-01 15:41:08 -05:00
|
|
|
//! Spawns things
|
2021-12-10 20:16:48 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-02-03 14:59:35 -05:00
|
|
|
use ::rltk::prelude::*;
|
2021-12-24 10:38:44 -05:00
|
|
|
use ::specs::prelude::*;
|
|
|
|
use ::specs::saveload::{MarkedBuilder, SimpleMarker};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
|
|
|
use crate::components::*;
|
2022-01-28 10:24:00 -05:00
|
|
|
use crate::random_table::MasterTable;
|
2022-01-25 09:58:30 -05:00
|
|
|
use crate::raws::{
|
|
|
|
get_spawn_table_for_depth, spawn_all_spells, spawn_named_entity, SpawnType, RAWS,
|
|
|
|
};
|
2022-02-03 14:59:35 -05:00
|
|
|
use crate::rng::roll_dice;
|
2022-01-18 11:00:13 -05:00
|
|
|
use crate::{colors, Map, MasterDungeonMap, 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 {
|
2022-01-25 09:58:30 -05:00
|
|
|
spawn_all_spells(ecs);
|
2022-01-04 11:11:38 -05:00
|
|
|
let player = ecs
|
|
|
|
.create_entity()
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(Position::from((player_x, player_y)))
|
2021-11-03 09:55:17 -04:00
|
|
|
.with(Renderable {
|
2022-02-03 14:59:35 -05:00
|
|
|
glyph: to_cp437('@'),
|
2022-01-14 12:19:46 -05:00
|
|
|
fg: colors::YELLOW,
|
|
|
|
bg: colors::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"))
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(HungerClock::default())
|
|
|
|
.with(Attributes::new(11))
|
2022-01-03 16:30:14 -05:00
|
|
|
.with(Skills::new(1))
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(Pools::player_pools())
|
2022-01-25 11:15:32 -05:00
|
|
|
.with(EquipmentChanged {})
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(LightSource::torch_light())
|
|
|
|
.with(Initiative::default())
|
2022-01-11 14:16:23 -05:00
|
|
|
.with(Faction::from("Player"))
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(KnownSpells::default())
|
2021-11-08 13:58:40 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-04 11:11:38 -05:00
|
|
|
.build();
|
|
|
|
|
|
|
|
// Starting equipment
|
2022-01-31 10:26:16 -05:00
|
|
|
let starting_equipment_carried = ["Dried Sausage", "Beer", "Shortbow"];
|
|
|
|
let starting_equipment_equipped = [
|
2022-01-04 11:11:38 -05:00
|
|
|
"Rusty Longsword",
|
|
|
|
"Stained Tunic",
|
|
|
|
"Torn Trousers",
|
|
|
|
"Old Boots",
|
2022-01-25 15:04:55 -05:00
|
|
|
];
|
2022-01-31 10:26:16 -05:00
|
|
|
for equipment in starting_equipment_carried.iter() {
|
2022-01-25 15:04:55 -05:00
|
|
|
spawn_named_entity(
|
|
|
|
&RAWS.lock().unwrap(),
|
|
|
|
ecs,
|
|
|
|
*equipment,
|
|
|
|
SpawnType::Carried { by: player },
|
|
|
|
);
|
|
|
|
}
|
2022-01-31 10:26:16 -05:00
|
|
|
for equipment in starting_equipment_equipped.iter() {
|
|
|
|
spawn_named_entity(
|
|
|
|
&RAWS.lock().unwrap(),
|
|
|
|
ecs,
|
|
|
|
*equipment,
|
|
|
|
SpawnType::Equipped { by: player },
|
|
|
|
);
|
|
|
|
}
|
2022-01-04 11:11:38 -05:00
|
|
|
|
2022-01-24 11:19:31 -05:00
|
|
|
// Starting hangover
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(StatusEffect { target: player })
|
|
|
|
.with(Duration { turns: 10 })
|
|
|
|
.with(Name::from("Hangover"))
|
2022-01-25 15:04:55 -05:00
|
|
|
.with(AttributeBonus::hangover())
|
2022-01-24 11:19:31 -05:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
|
2022-01-04 11:11:38 -05:00
|
|
|
player
|
2021-11-03 09:55:17 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:06:55 -05:00
|
|
|
const MAX_MONSTERS: i32 = 4;
|
|
|
|
|
2022-01-28 10:24:00 -05:00
|
|
|
fn room_table(map_depth: i32) -> MasterTable {
|
2021-12-23 13:07:50 -05:00
|
|
|
get_spawn_table_for_depth(&RAWS.lock().unwrap(), map_depth)
|
2021-11-12 14:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// fills a room with stuff!
|
|
|
|
#[allow(clippy::map_entry)]
|
2022-02-03 14:59:35 -05:00
|
|
|
pub fn spawn_room(map: &Map, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 14:59:35 -05:00
|
|
|
spawn_region(map, &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
|
|
|
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,
|
2022-02-03 14:59:35 -05:00
|
|
|
roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3,
|
2021-12-03 15:55:07 -05:00
|
|
|
);
|
|
|
|
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 {
|
2022-02-03 14:59:35 -05:00
|
|
|
(roll_dice(1, areas.len() as i32) - 1) as usize
|
2021-12-03 15:55:07 -05:00
|
|
|
};
|
|
|
|
let map_idx = areas[array_index];
|
2022-02-03 14:59:35 -05:00
|
|
|
spawn_points.insert(map_idx, spawn_table.roll());
|
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)) {
|
2022-01-25 11:15:32 -05:00
|
|
|
let width: usize;
|
|
|
|
{
|
|
|
|
width = ecs.fetch::<Map>().width as usize;
|
|
|
|
}
|
|
|
|
|
2022-01-21 11:57:36 -05:00
|
|
|
let (idx, name) = *spawn;
|
2022-01-25 11:42:02 -05:00
|
|
|
if name == "None" || name.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-21 11:57:36 -05:00
|
|
|
let x = (*idx % width) as i32;
|
|
|
|
let y = (*idx / width) as i32;
|
2021-12-23 12:31:03 -05:00
|
|
|
let item_result = spawn_named_entity(
|
2021-12-23 11:38:37 -05:00
|
|
|
&RAWS.lock().unwrap(),
|
2022-01-04 11:11:38 -05:00
|
|
|
ecs,
|
2022-01-21 11:57:36 -05:00
|
|
|
name,
|
2021-12-23 11:38:37 -05:00
|
|
|
SpawnType::AtPosition { x, y },
|
|
|
|
);
|
|
|
|
|
2022-01-25 11:42:02 -05:00
|
|
|
if item_result.is_none() {
|
2022-02-04 09:52:09 -05:00
|
|
|
#[cfg(feature = "debug")]
|
2022-02-03 14:59:35 -05:00
|
|
|
console::log(format!("WARNING: We don't know how to spawn [{}]!", name));
|
2022-01-25 11:15:32 -05:00
|
|
|
}
|
2021-12-17 13:53:14 -05:00
|
|
|
}
|
2022-01-18 11:00:13 -05:00
|
|
|
|
|
|
|
pub fn spawn_town_portal(ecs: &mut World) {
|
|
|
|
// Get current position & depth
|
|
|
|
let map = ecs.fetch::<Map>();
|
|
|
|
let player_depth = map.depth;
|
|
|
|
let player_pos = ecs.fetch::<Point>();
|
|
|
|
let player_x = player_pos.x;
|
|
|
|
let player_y = player_pos.y;
|
|
|
|
std::mem::drop(player_pos);
|
|
|
|
std::mem::drop(map);
|
|
|
|
|
|
|
|
// Find part of the town for the portal
|
|
|
|
let dm = ecs.fetch::<MasterDungeonMap>();
|
|
|
|
let town_map = dm.get_map(1).unwrap();
|
|
|
|
let mut stairs_idx = 0;
|
|
|
|
for (idx, tt) in town_map.tiles.iter().enumerate() {
|
|
|
|
if *tt == TileType::DownStairs {
|
|
|
|
stairs_idx = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let portal_x = (stairs_idx as i32 % town_map.width) - 2;
|
|
|
|
let portal_y = stairs_idx as i32 / town_map.width;
|
|
|
|
|
|
|
|
std::mem::drop(dm);
|
|
|
|
|
|
|
|
// Spawn the portal itself
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(OtherLevelPosition {
|
|
|
|
x: portal_x,
|
|
|
|
y: portal_y,
|
|
|
|
depth: 1,
|
|
|
|
})
|
|
|
|
.with(Renderable {
|
2022-02-03 14:59:35 -05:00
|
|
|
glyph: to_cp437('♥'),
|
2022-01-18 11:00:13 -05:00
|
|
|
fg: colors::CYAN,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
render_order: 0,
|
|
|
|
})
|
|
|
|
.with(EntryTrigger {})
|
|
|
|
.with(TeleportTo {
|
|
|
|
x: player_x,
|
|
|
|
y: player_y,
|
|
|
|
depth: player_depth,
|
|
|
|
player_only: true,
|
|
|
|
})
|
|
|
|
.with(Name::from("Town Portal"))
|
|
|
|
.with(SingleActivation {})
|
|
|
|
.build();
|
|
|
|
}
|