spawner.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. extern crate rltk;
  2. use rltk::{ RGB, RandomNumberGenerator };
  3. extern crate specs;
  4. use specs::prelude::*;
  5. use super::{CombatStats, Player, Renderable, Name, Position, Viewshed, Monster, BlocksTile, Rect, Item,
  6. Consumable, Ranged, ProvidesHealing, map::MAPWIDTH, InflictsDamage, AreaOfEffect, Confusion, SerializeMe,
  7. random_table::RandomTable, EquipmentSlot, Equippable, MeleePowerBonus, DefenseBonus, HungerClock,
  8. HungerState, ProvidesFood };
  9. use crate::specs::saveload::{MarkedBuilder, SimpleMarker};
  10. use std::collections::HashMap;
  11. /// Spawns the player and returns his/her entity object.
  12. pub fn player(ecs : &mut World, player_x : i32, player_y : i32) -> Entity {
  13. ecs
  14. .create_entity()
  15. .with(Position { x: player_x, y: player_y })
  16. .with(Renderable {
  17. glyph: rltk::to_cp437('@'),
  18. fg: RGB::named(rltk::YELLOW),
  19. bg: RGB::named(rltk::BLACK),
  20. render_order: 0
  21. })
  22. .with(Player{})
  23. .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
  24. .with(Name{name: "Player".to_string() })
  25. .with(CombatStats{ max_hp: 30, hp: 30, defense: 2, power: 5 })
  26. .with(HungerClock{ state: HungerState::WellFed, duration: 20 })
  27. .marked::<SimpleMarker<SerializeMe>>()
  28. .build()
  29. }
  30. const MAX_MONSTERS : i32 = 4;
  31. fn room_table(map_depth: i32) -> RandomTable {
  32. RandomTable::new()
  33. .add("Goblin", 10)
  34. .add("Orc", 1 + map_depth)
  35. .add("Health Potion", 7)
  36. .add("Fireball Scroll", 2 + map_depth)
  37. .add("Confusion Scroll", 2 + map_depth)
  38. .add("Magic Missile Scroll", 4)
  39. .add("Dagger", 3)
  40. .add("Shield", 3)
  41. .add("Longsword", map_depth - 1)
  42. .add("Tower Shield", map_depth - 1)
  43. .add("Rations", 10)
  44. }
  45. /// Fills a room with stuff!
  46. #[allow(clippy::map_entry)]
  47. pub fn spawn_room(ecs: &mut World, room : &Rect, map_depth: i32) {
  48. let spawn_table = room_table(map_depth);
  49. let mut spawn_points : HashMap<usize, String> = HashMap::new();
  50. // Scope to keep the borrow checker happy
  51. {
  52. let mut rng = ecs.write_resource::<RandomNumberGenerator>();
  53. let num_spawns = rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3;
  54. for _i in 0 .. num_spawns {
  55. let mut added = false;
  56. let mut tries = 0;
  57. while !added && tries < 20 {
  58. let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize;
  59. let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize;
  60. let idx = (y * MAPWIDTH) + x;
  61. if !spawn_points.contains_key(&idx) {
  62. spawn_points.insert(idx, spawn_table.roll(&mut rng));
  63. added = true;
  64. } else {
  65. tries += 1;
  66. }
  67. }
  68. }
  69. }
  70. // Actually spawn the monsters
  71. for spawn in spawn_points.iter() {
  72. let x = (*spawn.0 % MAPWIDTH) as i32;
  73. let y = (*spawn.0 / MAPWIDTH) as i32;
  74. match spawn.1.as_ref() {
  75. "Goblin" => goblin(ecs, x, y),
  76. "Orc" => orc(ecs, x, y),
  77. "Health Potion" => health_potion(ecs, x, y),
  78. "Fireball Scroll" => fireball_scroll(ecs, x, y),
  79. "Confusion Scroll" => confusion_scroll(ecs, x, y),
  80. "Magic Missile Scroll" => magic_missile_scroll(ecs, x, y),
  81. "Dagger" => dagger(ecs, x, y),
  82. "Shield" => shield(ecs, x, y),
  83. "Longsword" => longsword(ecs, x, y),
  84. "Tower Shield" => tower_shield(ecs, x, y),
  85. "Rations" => rations(ecs, x, y),
  86. _ => {}
  87. }
  88. }
  89. }
  90. fn orc(ecs: &mut World, x: i32, y: i32) { monster(ecs, x, y, rltk::to_cp437('o'), "Orc"); }
  91. fn goblin(ecs: &mut World, x: i32, y: i32) { monster(ecs, x, y, rltk::to_cp437('g'), "Goblin"); }
  92. fn monster<S : ToString>(ecs: &mut World, x: i32, y: i32, glyph : u8, name : S) {
  93. ecs.create_entity()
  94. .with(Position{ x, y })
  95. .with(Renderable{
  96. glyph,
  97. fg: RGB::named(rltk::RED),
  98. bg: RGB::named(rltk::BLACK),
  99. render_order: 1
  100. })
  101. .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
  102. .with(Monster{})
  103. .with(Name{ name : name.to_string() })
  104. .with(BlocksTile{})
  105. .with(CombatStats{ max_hp: 16, hp: 16, defense: 1, power: 4 })
  106. .marked::<SimpleMarker<SerializeMe>>()
  107. .build();
  108. }
  109. fn health_potion(ecs: &mut World, x: i32, y: i32) {
  110. ecs.create_entity()
  111. .with(Position{ x, y })
  112. .with(Renderable{
  113. glyph: rltk::to_cp437('¡'),
  114. fg: RGB::named(rltk::MAGENTA),
  115. bg: RGB::named(rltk::BLACK),
  116. render_order: 2
  117. })
  118. .with(Name{ name : "Health Potion".to_string() })
  119. .with(Item{})
  120. .with(Consumable{})
  121. .with(ProvidesHealing{ heal_amount: 8 })
  122. .marked::<SimpleMarker<SerializeMe>>()
  123. .build();
  124. }
  125. fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) {
  126. ecs.create_entity()
  127. .with(Position{ x, y })
  128. .with(Renderable{
  129. glyph: rltk::to_cp437(')'),
  130. fg: RGB::named(rltk::CYAN),
  131. bg: RGB::named(rltk::BLACK),
  132. render_order: 2
  133. })
  134. .with(Name{ name : "Magic Missile Scroll".to_string() })
  135. .with(Item{})
  136. .with(Consumable{})
  137. .with(Ranged{ range: 6 })
  138. .with(InflictsDamage{ damage: 20 })
  139. .marked::<SimpleMarker<SerializeMe>>()
  140. .build();
  141. }
  142. fn fireball_scroll(ecs: &mut World, x: i32, y: i32) {
  143. ecs.create_entity()
  144. .with(Position{ x, y })
  145. .with(Renderable{
  146. glyph: rltk::to_cp437(')'),
  147. fg: RGB::named(rltk::ORANGE),
  148. bg: RGB::named(rltk::BLACK),
  149. render_order: 2
  150. })
  151. .with(Name{ name : "Fireball Scroll".to_string() })
  152. .with(Item{})
  153. .with(Consumable{})
  154. .with(Ranged{ range: 6 })
  155. .with(InflictsDamage{ damage: 20 })
  156. .with(AreaOfEffect{ radius: 3 })
  157. .marked::<SimpleMarker<SerializeMe>>()
  158. .build();
  159. }
  160. fn confusion_scroll(ecs: &mut World, x: i32, y: i32) {
  161. ecs.create_entity()
  162. .with(Position{ x, y })
  163. .with(Renderable{
  164. glyph: rltk::to_cp437(')'),
  165. fg: RGB::named(rltk::PINK),
  166. bg: RGB::named(rltk::BLACK),
  167. render_order: 2
  168. })
  169. .with(Name{ name : "Confusion Scroll".to_string() })
  170. .with(Item{})
  171. .with(Consumable{})
  172. .with(Ranged{ range: 6 })
  173. .with(Confusion{ turns: 4 })
  174. .marked::<SimpleMarker<SerializeMe>>()
  175. .build();
  176. }
  177. fn dagger(ecs: &mut World, x: i32, y: i32) {
  178. ecs.create_entity()
  179. .with(Position{ x, y })
  180. .with(Renderable{
  181. glyph: rltk::to_cp437('/'),
  182. fg: RGB::named(rltk::CYAN),
  183. bg: RGB::named(rltk::BLACK),
  184. render_order: 2
  185. })
  186. .with(Name{ name : "Dagger".to_string() })
  187. .with(Item{})
  188. .with(Equippable{ slot: EquipmentSlot::Melee })
  189. .with(MeleePowerBonus{ power: 2 })
  190. .marked::<SimpleMarker<SerializeMe>>()
  191. .build();
  192. }
  193. fn shield(ecs: &mut World, x: i32, y: i32) {
  194. ecs.create_entity()
  195. .with(Position{ x, y })
  196. .with(Renderable{
  197. glyph: rltk::to_cp437('('),
  198. fg: RGB::named(rltk::CYAN),
  199. bg: RGB::named(rltk::BLACK),
  200. render_order: 2
  201. })
  202. .with(Name{ name : "Shield".to_string() })
  203. .with(Item{})
  204. .with(Equippable{ slot: EquipmentSlot::Shield })
  205. .with(DefenseBonus{ defense: 1 })
  206. .marked::<SimpleMarker<SerializeMe>>()
  207. .build();
  208. }
  209. fn longsword(ecs: &mut World, x: i32, y: i32) {
  210. ecs.create_entity()
  211. .with(Position{ x, y })
  212. .with(Renderable{
  213. glyph: rltk::to_cp437('/'),
  214. fg: RGB::named(rltk::YELLOW),
  215. bg: RGB::named(rltk::BLACK),
  216. render_order: 2
  217. })
  218. .with(Name{ name : "Longsword".to_string() })
  219. .with(Item{})
  220. .with(Equippable{ slot: EquipmentSlot::Melee })
  221. .with(MeleePowerBonus{ power: 4 })
  222. .marked::<SimpleMarker<SerializeMe>>()
  223. .build();
  224. }
  225. fn tower_shield(ecs: &mut World, x: i32, y: i32) {
  226. ecs.create_entity()
  227. .with(Position{ x, y })
  228. .with(Renderable{
  229. glyph: rltk::to_cp437('('),
  230. fg: RGB::named(rltk::YELLOW),
  231. bg: RGB::named(rltk::BLACK),
  232. render_order: 2
  233. })
  234. .with(Name{ name : "Tower Shield".to_string() })
  235. .with(Item{})
  236. .with(Equippable{ slot: EquipmentSlot::Shield })
  237. .with(DefenseBonus{ defense: 3 })
  238. .marked::<SimpleMarker<SerializeMe>>()
  239. .build();
  240. }
  241. fn rations(ecs: &mut World, x: i32, y: i32) {
  242. ecs.create_entity()
  243. .with(Position{ x, y })
  244. .with(Renderable{
  245. glyph: rltk::to_cp437('%'),
  246. fg: RGB::named(rltk::GREEN),
  247. bg: RGB::named(rltk::BLACK),
  248. render_order: 2
  249. })
  250. .with(Name{ name : "Rations".to_string() })
  251. .with(Item{})
  252. .with(ProvidesFood{})
  253. .with(Consumable{})
  254. .marked::<SimpleMarker<SerializeMe>>()
  255. .build();
  256. }