spawner.rs 9.8 KB

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