You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.9 KiB

5 years ago
5 years ago
  1. use std::collections::BTreeMap;
  2. use serde::{Serialize, Deserialize};
  3. use rand::Rng;
  4. use rand::distributions::{WeightedIndex, Distribution};
  5. use entity::item::tool::{Tool, ToolType};
  6. use maps::room::{Difficulty, Episode};
  7. use maps::area::MapArea;
  8. use entity::character::SectionID;
  9. use crate::{ItemDropType, load_data_file};
  10. use crate::tech_table::TechniqueTable;
  11. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, enum_utils::FromStr)]
  12. enum ToolRateType {
  13. Monomate,
  14. Dimate,
  15. Trimate,
  16. Monofluid,
  17. Difluid,
  18. Trifluid,
  19. Antidote,
  20. Antiparalysis,
  21. SolAtomizer,
  22. MoonAtomizer,
  23. StarAtomizer,
  24. Telepipe,
  25. TrapVision,
  26. Monogrinder,
  27. Digrinder,
  28. Trigrinder,
  29. PowerMaterial,
  30. MindMaterial,
  31. EvadeMaterial,
  32. HpMaterial,
  33. TpMaterial,
  34. DefMaterial,
  35. LuckMaterial,
  36. ScapeDoll,
  37. Technique,
  38. PhotonDrop,
  39. }
  40. /*#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
  41. struct ToolRate {
  42. tool: ToolRateType,
  43. rate: u32,
  44. }*/
  45. #[derive(Debug, Serialize, Deserialize)]
  46. struct ToolRatesRaw {
  47. area1: BTreeMap<String, u32>,
  48. area2: BTreeMap<String, u32>,
  49. area3: BTreeMap<String, u32>,
  50. area4: BTreeMap<String, u32>,
  51. area5: BTreeMap<String, u32>,
  52. area6: BTreeMap<String, u32>,
  53. area7: BTreeMap<String, u32>,
  54. area8: BTreeMap<String, u32>,
  55. area9: BTreeMap<String, u32>,
  56. area10: BTreeMap<String, u32>,
  57. }
  58. struct ToolRates {
  59. area1: BTreeMap<ToolRateType, u32>,
  60. area2: BTreeMap<ToolRateType, u32>,
  61. area3: BTreeMap<ToolRateType, u32>,
  62. area4: BTreeMap<ToolRateType, u32>,
  63. area5: BTreeMap<ToolRateType, u32>,
  64. area6: BTreeMap<ToolRateType, u32>,
  65. area7: BTreeMap<ToolRateType, u32>,
  66. area8: BTreeMap<ToolRateType, u32>,
  67. area9: BTreeMap<ToolRateType, u32>,
  68. area10: BTreeMap<ToolRateType, u32>,
  69. }
  70. impl ToolRates {
  71. fn new(raw: ToolRatesRaw) -> ToolRates {
  72. ToolRates {
  73. area1: raw.area1.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  74. area2: raw.area2.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  75. area3: raw.area3.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  76. area4: raw.area4.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  77. area5: raw.area5.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  78. area6: raw.area6.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  79. area7: raw.area7.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  80. area8: raw.area8.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  81. area9: raw.area9.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  82. area10: raw.area10.into_iter().map(|(name, rate)| (name.parse().unwrap(), rate)).collect(),
  83. }
  84. }
  85. }
  86. impl ToolRates {
  87. fn get_by_area<'a>(&'a self, map_area: &MapArea) -> &'a BTreeMap<ToolRateType, u32> {
  88. match map_area.drop_area_value().unwrap() {
  89. 0 => &self.area1,
  90. 1 => &self.area2,
  91. 2 => &self.area3,
  92. 3 => &self.area4,
  93. 4 => &self.area5,
  94. 5 => &self.area6,
  95. 6 => &self.area7,
  96. 7 => &self.area8,
  97. 8 => &self.area9,
  98. _ => &self.area10,
  99. }
  100. }
  101. }
  102. pub struct ToolTable {
  103. rates: ToolRates,
  104. tech_table: TechniqueTable,
  105. }
  106. impl ToolTable {
  107. pub fn new(episode: Episode, difficulty: Difficulty, section_id: SectionID) -> ToolTable {
  108. let rates = load_data_file(episode, difficulty, section_id, "tool_rate.toml");
  109. ToolTable {
  110. rates: ToolRates::new(rates),
  111. tech_table: TechniqueTable::new(episode, difficulty, section_id),
  112. }
  113. }
  114. pub fn get_drop<R: Rng>(&self, map_area: &MapArea, rng: &mut R) -> Option<ItemDropType> {
  115. let tool_rates = self.rates.get_by_area(map_area).iter();
  116. let tool_weights = WeightedIndex::new(tool_rates.clone().map(|(_, weights)| weights)).unwrap();
  117. let tool = tool_rates.map(|(ttype, _)| ttype).nth(tool_weights.sample(rng)).unwrap();
  118. let tool_type = match tool {
  119. ToolRateType::Monomate => ToolType::Monomate,
  120. ToolRateType::Dimate => ToolType::Dimate,
  121. ToolRateType::Trimate => ToolType::Trimate,
  122. ToolRateType::Monofluid => ToolType::Monofluid,
  123. ToolRateType::Difluid => ToolType::Difluid,
  124. ToolRateType::Trifluid => ToolType::Trifluid,
  125. ToolRateType::Antidote => ToolType::Antidote,
  126. ToolRateType::Antiparalysis => ToolType::Antiparalysis,
  127. ToolRateType::SolAtomizer => ToolType::SolAtomizer,
  128. ToolRateType::MoonAtomizer => ToolType::MoonAtomizer,
  129. ToolRateType::StarAtomizer => ToolType::StarAtomizer,
  130. ToolRateType::Telepipe => ToolType::Telepipe,
  131. ToolRateType::TrapVision => ToolType::TrapVision,
  132. ToolRateType::Monogrinder => ToolType::Monogrinder,
  133. ToolRateType::Digrinder => ToolType::Digrinder,
  134. ToolRateType::Trigrinder => ToolType::Trigrinder,
  135. ToolRateType::PowerMaterial => ToolType::PowerMaterial,
  136. ToolRateType::MindMaterial => ToolType::MindMaterial,
  137. ToolRateType::EvadeMaterial => ToolType::EvadeMaterial,
  138. ToolRateType::HpMaterial => ToolType::HpMaterial,
  139. ToolRateType::TpMaterial => ToolType::TpMaterial,
  140. ToolRateType::DefMaterial => ToolType::DefMaterial,
  141. ToolRateType::LuckMaterial => ToolType::LuckMaterial,
  142. ToolRateType::ScapeDoll => ToolType::ScapeDoll,
  143. ToolRateType::PhotonDrop => ToolType::PhotonDrop,
  144. ToolRateType::Technique => return self.tech_table.get_drop(map_area, rng),
  145. };
  146. Some(ItemDropType::Tool(Tool {
  147. tool: tool_type
  148. }))
  149. }
  150. }